R 转换为带“的数值列”;空";价值观
我已经将一个数据集导入到R中,其中一列中应该包含R 转换为带“的数值列”;空";价值观,r,class,character,transformation,numeric,R,Class,Character,Transformation,Numeric,我已经将一个数据集导入到R中,其中一列中应该包含数值值NULL。这将使R根据是否使用stringAsFactors参数,将列类设置为character或factor 为了让您了解这是数据集的结构 > str(data) 'data.frame': 1016 obs. of 10 variables: $ Date : Date, format: "2014-01-01" "2014-01-01" "2014-01-01" "2014-01-01" ... $ Name
数值
值NULL
。这将使R根据是否使用stringAsFactors
参数,将列类设置为character
或factor
为了让您了解这是数据集的结构
> str(data)
'data.frame': 1016 obs. of 10 variables:
$ Date : Date, format: "2014-01-01" "2014-01-01" "2014-01-01" "2014-01-01" ...
$ Name : chr "Chi" "Chi" "Chi" "Chi" ...
$ Impressions: chr "229097" "3323" "70171" "1359" ...
$ Revenue : num 533.78 11.62 346.16 3.36 1282.28 ...
$ Clicks : num 472 13 369 1 963 161 1 7 317 21 ...
$ CTR : chr "0.21" "0.39" "0.53" "0.07" ...
$ PCC : chr "32" "2" "18" "0" ...
$ PCOV : chr "3470.52" "94.97" "2176.95" "0" ...
$ PCROI : chr "6.5" "8.17" "6.29" "NULL" ...
$ Dimension : Factor w/ 11 levels "100x72","1200x627",..: 1 3 4 5 7 8 9 10 11 1 ...
我想将PCROI列转换为数字,但如果包含NULL
s,这会使转换更加困难。
我试图回避将值0
设置为当前值为NULL
的所有观察值的问题,但我收到以下错误消息:
> data$PCROI[which(data$PCROI == "NULL"), ] <- 0
Error in data$PCROI[which(data$PCROI == "NULL"), ] <- 0 :
incorrect number of subscripts on matrix
>data$PCROI[其中(data$PCROI==“NULL”),]出现语法错误:
data$PCROI[which(data$PCROI == "NULL"), ] <- 0 # will not work
data$PCROI[which(data$PCROI == "NULL")] <- 0 # will work
它会自动将您的“NULL”转换为NA。我不知道为什么从一开始就没有尝试data$PCROI=as.numeric(data$PCROI)
(!?)。谢谢你,汉斯!
data$PCROI = as.numeric(data$PCROI)