0
votes

I am using inbuilt data set "mtcars" in R. I have converted it into data.table format. I have another data.table, m1, of NA values. dimension of m1 is same as dimension of mtcars, say, the dimension is 32X11. My objective is to save some rows of mtcars in m1

(i.e., m1[c(1,11,13),]<-mtcars[c(1,11,13),])

using data.table. But whenever i am trying to do that, it is giving TRUE/FALSE values for 1,11,13 rows of m1. How do I save 1,11,13 rows of mtcars in m1? (Note: I want to use data.table format only). Any help will be appreciated. Thanks in advance!!

1

1 Answers

5
votes

You shouldn't do things like that at all (there is probably a better way for your objective), but you need to fill m1 with NA_real_ values.

library(data.table)
DT <- data.table(mtcars)

m1 <- data.table(matrix(NA, nrow=32, ncol=11))
#fills m1 with NA_logical_ values
m1[c(1,11,13),] <- DT[c(1,11,13),]
#warnings and logical values in result

m1 <- data.table(matrix(NA_real_, nrow=32, ncol=11))
m1[c(1,11,13),] <- DT[c(1,11,13),]
#works

m1 <- data.table(matrix(NA_real_, nrow=32, ncol=11))
m1[c(1,11,13), colnames(m1) := DT[c(1,11,13),]]
#assigns by reference

Obviously, if you have mixed data types, you also need mixed NA types. Read help("NA").