如何在R中将所有行合并为单个行? [英] How to combine all rows into a single row in R?
本文介绍了如何在R中将所有行合并为单个行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含100行和10列的数据框,我想知道如何将所有100行合并为仅一行?
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 ……
推荐答案
我们可以获得转置
并连接到 vector
。请注意,转置
会转换为矩阵
,因此无需调用 as.matrix
。
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))
或者我们也可以在将行拆分为列表后使用unlist。
Or we can use unlist after splitting the rows into a list.
unlist(lapply(seq_len(nrow(mydata)), function(i) mydata[i,]))
这篇关于如何在R中将所有行合并为单个行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文