There are several issues here.
The first is the data.table property, that they do not have rownames , instead they have key , which are much more powerful. See this wonderful vignette .
But this is not the end of the world. model.matrix returns sensitive model.matrix names when you pass it to data.table
for instance
A <- data.table(ID = 1:5, x = c(NA, 1:4), y = c(4:2,NA,3)) mm <- model.matrix( ~ x + y, A) rownames(mm) ## [1] "2" "3" "5"
So lines 2,3 and 5 are those included in model.matrix.
Now you can add this sequence as a column to A This will be useful if you then set the key to something else (thereby losing the original order)
A[, rowid := seq_len(nrow(A)]
You might consider making it a character (e.g., the names of the growths in mm )), but that doesn't really matter (since you can just as easily convert rownames(mm) to numeric when you need to refer.
Regarding the warning given by data.table if you read the following sentence
Avoid the key <-, names <- and attr <- which in R currently (and weirdly) can copy the entire data table. Use the set * syntax instead to avoid copying: setkey (), setnames () and setattr ()
rownames are an attribute of rownames<- (internally at some point using the equivalent of attr<- ) will (possibly copy) in the same way.
The line from `row.names<-.data.frame` is equal to
attr(x, "row.names") <- value
At the same time, data.tables do not have data.tables names, so it makes no sense to set them.