英文:
Convert data frame from character to numeric
问题
我有以下包含一个列的数据框,该列当前存储为字符列:
我试图分离文本,但似乎separate()函数不适用于字符列。
我尝试使用以下代码将列转换,但都不适用于我。
第一次尝试:
Overview_10_K_filings_df$Overview_10_K_filings <- as.numeric(as.character(Overview_10_K_filings_df$Overview_10_K_filings))
这会创建错误消息:“警告信息:强制转换引入了NA”。
第二次尝试:
Overview_10_K_filings_df[1] <- apply(Overview_10_K_filings_df[1], 2,
function(x) as.numeric(as.character(x))
你可以帮助我转换这一列吗?或者是否有其他方法可以分离内容?
谢谢!
英文:
I have the following data frame with one column, which is currently stored as a character column:
I am trying to separate the text, but it seems like the separate() function doesn't work on character columns.
I tried to covert the columns, using the following codes. Neither of them works for me.
First try:
Overview_10_K_filings_df$Overview_10_K_filings <- as.numeric(as.character(Overview_10_K_filings_df$Overview_10_K_filings))
This creates the error message: "Warning message: NAs introduced by coercion"
Second try:
Overview_10_K_filings_df[1] <- apply(Overview_10_K_filings_df[1], 2,
function(x) as.numeric(as.character(x))
Can you help me to transform the column? Or is there any other way that I can separate the content?
Thanks!
答案1
得分: 1
通过将字符串转换为DF并在3个步骤中使用str_replace
来实现。也许不是实现目标的最简洁方式。这三个步骤被保留在DF中,以说明替换的过程。
library(tidyverse)
t <- "QTR4/20151229_10-K_edgar_data_1230058_0000892626-15-000373.txt"
t |> as.data.frame() |>
mutate(new1=stringr::str_replace(t, '/', ' | ')) |>
mutate(new2 = stringr::str_replace_all(new1, '_', ' | ')) |>
mutate(new3 = stringr::str_replace(new2, '.txt', ' | txt')) |>
select(new3) |> as.character()
#> [1] "QTR4 | 20151229 | 10-K | edgar | data | 1230058 | 0000892626-15-000373 | txt"
更好的方式:
或者你可以这样做:
b <- "_|/|\\."
stringr::str_replace_all(t, b, ' | ')
# [1] "QTR4 | 20151229 | 10-K | edgar | data | 1230058 | 0000892626-15-000373 | txt"
英文:
By creating a DF out of the string and using str_replace
in 3 steps.
Maybe not the most concise way of achieving the goal. The three steps are kept in the DF for informative reasons how the replacing goes.
library(tidyverse)
t <- "QTR4/20151229_10-K_edgar_data_1230058_0000892626-15-000373.txt"
t |> as.data.frame() |>
mutate(new1=stringr::str_replace(t, '/', ' | ')) |>
mutate(new2 = stringr::str_replace_all(new1, '_', ' | ')) |>
mutate(new3 = stringr::str_replace(new2, '.txt', ' | txt')) |>
select(new3) |> as.character()
#> [1] "QTR4 | 20151229 | 10-K | edgar | data | 1230058 | 0000892626-15-000373 | txt"
Better:
Or you do it this way:
b <- "_|/|\\."
stringr::str_replace_all(t, b, ' | ')
# [1] "QTR4 | 20151229 | 10-K | edgar | data | 1230058 | 0000892626-15-000373 | txt"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论