3

我有一个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
4

0 回答 0