I want to turn data.table
rows into vectors. Here's what worked for me:
unlist(dt[row_num])
But is there a more native solution? I also don't like that the above retains the name when really I want a pure numeric vector instead, which then leads to:
as.numeric(unlist(dt[row_num]))
Seems like there should be a better option.
Ok, now I know you want a row:
as.matrix(dt[row_num])[1,]
IMO it is better to use first the data.table
-operation and not to convert the complete datatable to a matrix. Simply the performance is better (especially on very large data.tables). Example:
library("data.table")
Iris <- data.table(iris[-5])
as.matrix(Iris[42])[1,]
The problem with extracting rows as vectors is that vectors are homogeneous while rows of data frames or data tables are not.
However, you can convert the data to a matrix then extract the row:
> x <- iris[1:10,1:4]
> as.matrix(x)[1,]
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.1 3.5 1.4 0.2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With