将数据帧转换为向量(按行) [英] Convert a dataframe to a vector (by rows)

查看:161
本文介绍了将数据帧转换为向量(按行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有此类数字条目的数据框

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)).请注意,如果要按列进行操作,则应使用unlist(test).

You can try as.vector(t(test)). Please note that, if you want to do it by columns you should use unlist(test).

这篇关于将数据帧转换为向量(按行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆