Why is the error from na.omit after starting scale in R in data.table?

I asked this question before and thought that I could post an example that shows why I saw the effect, in case this might be useful:

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 is that scale does not return a vector and data.table did not complain. Running x[,a:=as.vector(scale(a))] instead fixes the problem. Am I missing something in the documentation?

+4
source share
1 answer

Good question (I edited a bit). This should be either a warning, or data.table can automatically force 1 matrix column for a vector, since I believe that scale in how you showed is a common task and it’s natural to do this. The na.omit aspect is one way to identify the problem, but there are probably other ways, as the main reason is even more, as you have shown well.

Error report sent, thanks:

Error # 2333 := is able to create a matrix column, but the matrix columns are not valid


Update : the root cause is now fixed in version 1.8.3. A matrix with 1 column is silently perceived as a vector. A matrix with two or more columns gives a warning.

+3
source

Source: https://habr.com/ru/post/1442173/


All Articles