我正在处理一个数据框('df_temp'),其中包含两列客户 ID('Custid')和收入('Income'):
Custid Income
<fctr> <dbl>
1 1003 29761.20
2 1004 98249.55
3 1006 23505.30
4 1007 72959.25
5 1009 114973.95
6 1010 25038.30
在检查收入是否为数字时,我面临以下问题:
使用 $ 引用 Income,返回 TRUE:
> is.numeric(df_temp$Income)
[1] TRUE
使用 [2] 或 [which(...)] 引用 Income,返回 FALSE:
> i <- which(names(df_temp)=='Income')
> is.numeric(df_temp[,i])
[1] FALSE
> is.numeric(df_temp[,2])
[1] FALSE
当尝试使用 [] 将此向量设置为数值时,我遇到了另一个问题:
> df_temp[,2] <- as.numeric(df_temp[,2])
Error: (list) object cannot be coerced to type 'double'
我一直认为在引用数据框中的向量时 $ 和 [] 具有相同的用途。
有人可以帮助我理解问题并使用 [] 表达式将此向量转换为数值吗?