用R数据框列中的NA替换字符串

时间:2018-11-04 21:48:30

标签: r

我很难找到用r语言的NA替换数据帧列中的字符串值的语法。整个列由1和0组成,但是由于这2个字符串离群值,整个列被注册为字符而不是数字。我想将这些离群的字符串转换为NA,然后将整个列转换为数值。

这是频率表列的输出

Here is the output from the frequency table column

我已经从汽车包中下载了recode()工具,但无法使其与字符值一起使用。

让我知道是否有人可以提供帮助!

1 个答案:

答案 0 :(得分:0)

给出向量x

x <- c(1, 0, "45 mins", "10 mins")
x
#[1] "1"       "0"       "45 mins" "10 mins"

你可以做

as.numeric(x)
#[1]  1  0 NA NA
# Warning message:
# NAs introduced by coercion 

所以尝试

dataframe$column <- as.numeric(dataframe$column)
相关问题