Why error from na.omit after running scale in R in data.table?
data.table, r
Solution
Good question (I edited slightly). It should either be a warning, or `data.table` could coerce the 1 column `matrix` to a vector automatically, since I guess `scale` in the way you showed is a common task and natural to do like that. The `na.omit` aspect is one way of revealing the issue, but there are probably other ways since the root cause is further up as you nicely showed.
Bug report filed, thanks :
Bug#2333 `:=` is able to create a "matrix" column, but "matrix" columns are invalid
Update: Root cause is now fixed in v1.8.3. A 1 column matrix is silently treated as vector. A matrix with 2 or more columns gives a warning.
Problem
I had asked this question earlier and thought I might post an example that shows why I was seeing the effect, in the event that it might be helpful: ``` require(data.table) x <- data.table(a=1:10) x[,a:=scale(a)] # [,1] # 1: -1.4863011 # 2: -1.1560120 # 3: -0.8257228 # 4: -0.4954337 # 5: -0.1651446 # 6: 0.1651446 # 7: 0.4954337 # 8: 0.8257228 # 9: 1.1560120 #10: 1.4863011 na.omit(x) 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. ``` The reason seems to be that `scale` does not return a `vector` and `data.table` did not complain. Doing `x[,a:=as.vector(scale(a))]` instead appears to fix the issue. Have I missed something in the documentation?