Converting the row of a data.table to a vector

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 

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,]

Tags:

R

Data.Table