如何将所有行组合成一行? [英] How to combine all rows into a single row?

查看:107
本文介绍了如何将所有行组合成一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧,它有100行和10列,我想知道如何将所有的100行合并成一行?谢谢。

  mydata = seq(1,1000)
mydata = as.data.frame(matrix(mydata, nrow = 100,ncol = 10,byrow = T))

结果应该是这样的:(只有一行)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...


解决方案>

我们可以获得转置并连接到向量。请注意,转置转换为矩阵,无需调用 as.matrix

  as.vector(t(mydata))

或者我们可以在将列分成列表后使用unlist。

  unlist(lapply(seq_len(n)(mydata)),function(i)mydata [i,]))


I have a dataframe which has 100 rows and 10 columns, i wonder how I can merge all the 100 rows into just one row? Thanks.

mydata=seq(1,1000)
mydata=as.data.frame(matrix(mydata,nrow = 100,ncol = 10,byrow=T))

the result should be like this:(just a single row)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ……

解决方案

We can get the transpose and concatenate to a vector. Note that the transpose converts to a matrix and there is no need to call as.matrix.

as.vector(t(mydata))

Or we can use unlist after splitting the rows into a list.

unlist(lapply(seq_len(nrow(mydata)), function(i) mydata[i,]))

这篇关于如何将所有行组合成一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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