Use melt.data.table()
, by specifying the row, along with the columns you want to extract (use measure.vars = 1:ncol(iris.dt)
for the whole row). Select the values by adding [, value]
on the end. It's best to specify columns that are all of the same type, or all the values will be converted to character (with a warning).
library("data.table")
iris.dt <- data.table(iris)
# One line solution:
row1 <- melt.data.table(iris.dt[1], measure.vars = 1:4)[, value]
# In steps:
iris.row1 <- iris.dt[1]
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1: 5.1 3.5 1.4 0.2 setosa
iris.melted <- melt.data.table(iris.row1, measure.vars = 1:4)
# variable value
# 1: Sepal.Length 5.1
# 2: Sepal.Width 3.5
# 3: Petal.Length 1.4
# 4: Petal.Width 0.2
row1 <- iris.melted[, value]
# [1] "5.1" "3.5" "1.4" "0.2"
list
tovector
in R; also note theuse.names
argument ofunlist
unname
.c(dt[row_num])