0

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 ……
Yang Yang
  • 858
  • 3
  • 26
  • 49
  • What do you expect the output to look like? Summed, concatenated, nested vectors, or something else? – r2evans Jul 15 '16 at 02:40
  • I have edited my question, thanks. – Yang Yang Jul 15 '16 at 02:42
  • 1
    `as.data.frame(matrix(mydata, nrow = 1))`? – r2evans Jul 15 '16 at 02:44
  • Sorry, but it is not the right answer. – Yang Yang Jul 15 '16 at 02:45
  • Then perhaps you can provide some more information? My suggestion includes all of your data in a single row. (Perhaps you can present a more [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to better explain what you are expecting.) – r2evans Jul 15 '16 at 02:47
  • Your answer will divide `mydata` into one row. But it includes ten variables and each of them contains 10 values. That is not what I am asking. Thanks. – Yang Yang Jul 15 '16 at 02:52

2 Answers2

4

Matrix are indexed by column so I am guessing you may just need to transpose your data and then construct a new matrix from it:

matrix(t(as.matrix(mydata)), nrow = 1)

Or you can convert it to a vector after the transposing depending on what you need:

as.vector(t(as.matrix(mydata)))
Psidom
  • 209,562
  • 33
  • 339
  • 356
2

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,]))
akrun
  • 874,273
  • 37
  • 540
  • 662