数据表中的na.omit,R

时间:2012-10-22 18:34:21

标签: r data.table na

这可能是一个潜在的错误,我很难使用一个独立的例子来复制它,但是,错误信息可能有助于深入挖掘它的底部。 mivdata.table

> str(miv)
Classes ‘data.table’ and 'data.frame':  113534 obs. of  21 variables:
 $ ym            : num  1980 1980 1980 1980 1980 ...
 $ dte           : Date, format: "1980-03-31" "1980-03-31" ...
 $ xs_ret        : num  -16.3 -16.3 -16.3 14.3 14.3 ...
 $ xs_mkt        : num  -8.85 -8.85 -8.85 12.77 12.77 ...
 $ smb           : num  -7.23 -7.23 -7.23 3.62 3.62 ...
 $ hml           : num  0.966 0.966 0.966 0.318 0.318 ...
 $ umd           : num  3.713 3.713 3.713 -0.762 -0.762 ...
 $ wficn         : num  100567 101394 102565 100010 100019 ...
 $ iv_w          : num [1:113534, 1] -1.039 -0.571 -0.831 -0.357 -0.263 ...

> miv <- na.omit(miv)
Error in `[.data.table`(object, !omit) : 
   i is invalid type (matrix). Perhaps in future a 2 column matrix could return a list of     elements of DT (in the spirit of A[B] in FAQ 2.14). Please let datatable-help know if you'd     like this, or add your comments to FR #1611.

1 个答案:

答案 0 :(得分:2)

如果它不可再现,则它太局部化了:

DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
DT[3, 2] <- NA
DT[4, 3] <- NA
DT
#   x  y  v
#1: a  1  1
#2: a  3  2
#3: a NA  3
#4: b  1 NA
#5: b  3  5
#6: b  6  6
#7: c  1  7
#8: c  3  8
#9: c  6  9
na.omit(DT)
#   x y v
#1: a 1 1
#2: a 3 2
#3: b 3 5
#4: b 6 6
#5: c 1 7
#6: c 3 8
#7: c 6 9