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