我试图简单地将数据帧转换为向量。这是一个样本:
mydataframe <- data.frame(1:4,5:8,9:12) #create data frame
myvector <- as.vector(mydataframe)
class(myvector) #this is not a vector
答案 0 :(得分:0)
对于该类型的数据框,您可以使用unlist
创建包含名称的向量,并使用unname(unlist())
删除名称。如:
mydataframe <- data.frame(1:4,5:8,9:12) #create data frame
myvector <- unname(unlist(mydataframe)) #create vector from dataframe
myvector
is.vector(myvector)
unlist
适用于这种情况,因为它将数据框简化为列表结构,而as.vector
仅删除属性。有关unlist
有时优先于as.vector