我有一个数据表DT2
,如下所示:
custName count_ Value _By_custName Mean_ Value _By_custName Median_ Value _By_custName Min_ Value _By_custName Max_ Value _By_custName Stdev_ Value _By_custName
1: Aaron Edwards 1 1008.00 1008.00 1008.00 1008.00 NA
2: Abigail Cunningham 1 754.92 754.92 754.92 754.92 NA
3: Abraham Mcguire 1 247.38 247.38 247.38 247.38 NA
4: Acton Mendoza 1 468.00 468.00 468.00 468.00 NA
5: Acton Ratliff 1 456.00 456.00 456.00 456.00 NA
---
994: Zia Ayala 1 2087.91 2087.91 2087.91 2087.91 NA
995: Zia Mcmillan 1 0.00 0.00 0.00 0.00 NA
996: Zorita Hodge 1 2624.50 2624.50 2624.50 2624.50 NA
997: Zorita Petty 1 708.50 708.50 708.50 708.50 NA
998: Zorita Vincent 1 564.00 564.00 564.00 564.00 NA
diff_ Value _By_custName pctdiff_ Value _By_custName
1: NaN NaN
2: NaN NaN
3: NaN NaN
4: NaN NaN
5: NaN NaN
---
994: NaN NaN
995: NaN NaN
996: NaN NaN
997: NaN NaN
998: NaN NaN
当我这样做时:
cnames = colnames(as.data.frame(DT2))
并运行代码:
L = length(cnames)
for( i in 1:L){
.t = as.symbol(cnames[i])
if(all(is.na(DT2[,eval(.t)]))){
.del = substitute(x:=NULL, list(x=.t))
DT2[, eval(.del)]
} else if(any(is.na(DT2[,eval(.t)]))){
.s0 = substitute(!is.na(x), list(x=.t))
mu = -99
.s1 = substitute(is.na(x), list(x=.t))
.s2 = substitute(x := y, list(x=.t, y=eval(mu)))
DT2 = DT2[eval(.s1), eval(.s2)]
}
}
这有效!
但是当我这样做时:
cnames = colnames(DT2)
代码因错误而崩溃:
Error in eval(expr, envir, enclos) : object 'NA' not found
基本上,cnames向量会删除术语!如果我从DT2中删除列,则也会删除cnames中的相应元素。为什么会这样?它是在背景中发生的一些指针吗?
修改
根据要求复制的例子:
> DT2 = data.table(x=c(1,2,3,4,5), y=c(10,20,30,40,50), z = rep(NA,5), u = rep(NaN, 5), w = rep(NaN,5))
> DT2
x y z u w
1: 1 10 NA NaN NaN
2: 2 20 NA NaN NaN
3: 3 30 NA NaN NaN
4: 4 40 NA NaN NaN
5: 5 50 NA NaN NaN
> cnames = colnames(DT2)
> L = length(cnames)
> for( i in 1:L){
+ print("===========")
+ print(cnames)
+ .t = as.symbol(cnames[i])
+ if(all(is.na(DT2[,eval(.t)]))){
+ .del = substitute(x:=NULL, list(x=.t))
+ DT2[, eval(.del)]
+ } else if(any(is.na(DT2[,eval(.t)]))){
+ .s0 = substitute(!is.na(x), list(x=.t))
+ mu = 3
+ .s1 = substitute(is.na(x), list(x=.t))
+ .s2 = substitute(x := y, list(x=.t, y=eval(mu)))
+ DT2 = DT2[eval(.s1), eval(.s2)]
+ }
+ print(cnames)
+ }
[1] "==========="
[1] "x" "y" "z" "u" "w"
[1] "x" "y" "z" "u" "w"
[1] "==========="
[1] "x" "y" "z" "u" "w"
[1] "x" "y" "z" "u" "w"
[1] "==========="
[1] "x" "y" "z" "u" "w"
[1] "x" "y" "u" "w"
[1] "==========="
[1] "x" "y" "u" "w"
[1] "x" "y" "u"
[1] "==========="
[1] "x" "y" "u"
Error in eval(expr, envir, enclos) : object 'NA' not found