将数据帧转换为向量(按行) [英] Converting a dataframe to a vector (by rows)
本文介绍了将数据帧转换为向量(按行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,带有这样的数字输入
I have a dataframe with numeric entries like this one
test <- data.frame(x=c(26,21,20),y=c(34,29,28))
以下矢量?
> 26,34,21,29,20,28
我能够使用以下内容获得它,但我想应该有一个更优雅的方式
I was able to get it using the following, but I guess there should be a much more elegant way
X <- test[1,]
for (i in 2:dim(test)[1]){
X <- cbind(X,test[i,])
}
推荐答案
您可以尝试 as.vector(t(test))
请注意,如果您想使用列,您应该使用 as.vector(as.matrix(test))
( t
隐式转换为矩阵)。
You can try as.vector(t(test))
.Please note that, if you want to have by columns you should use as.vector(as.matrix(test))
(t
implicitly converts to a matrix).
这篇关于将数据帧转换为向量(按行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文