我需要以向量的形式从data.table中提取一列。我知道$
有效,[]
无效。但是,as.vector()
在单个维度的data.table 对象上失败。
library(data.table)
dt <- iris
setDT(dt)
oneCol1 <- dt$Sepal.Length[1:100] # produces a vector of 100 elements, fine
oneCol2 <- dt[1:100,1] # produces a DT 100 rows by 1 column. I understand limitation.
v <- as.vector(oneCol2)
is.vector(v) # FALSE
该文件说as.vector()
"试图胁迫......x,一个 R 对象。不过,什么也没发生。v
仍然是 DT 对象。
当然,我可以使用第一种方法,但我想知道为什么as.vector()
不起作用。
数据框是列表。即使您选择了一行数据,它仍被视为一个列表。
请改用取消列出:
library(data.table)
dt <- iris
setDT(dt)
oneCol1 <- dt$Sepal.Length[1:100] # produces a vector of 100 elements, fine
oneCol2 <- dt[1:100,1] # produces a DT 100 rows by 1 column. I understand limitation.
t<-unlist(oneCol2)
is.vector(t) # true