2

I'd like to turn my rows into a single column. I tried with VBA but it doesn't go all the way (it crashes before). So I'm thinking of trying on R.

More precisely I have 193 columns and 4957 rows. So I would like to get one column of 956,701 rows. So I need an automatic code, not a formula that has to be adapted because it would take too long.

So just to make sure I've made myself clear, here's what I'd like to get for this little example:

1 2 3 4 5
6 7 8 9 10

1
2
3
4
5
6
7
8
9
10

Do you have any idea what code might solve my problem?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453

2 Answers2

1

We can transpose and concatenate

newdf <- data.frame(col1 = c(t(df1)))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • (1) will this work if the OP starts from a data frame? (Not clear if they've got a matrix or a df.) (2) do you need `as.data.frame(matrix(...))` or does just `data.frame()` work? – Ben Bolker May 07 '20 at 23:30
  • @BenBolker the `matrix/as.data.frame` is unnecessary as you stated. Inititally, I had only `matrix` – akrun May 07 '20 at 23:32
  • 1
    and yes, you can transpose a homogeneous data frame (to my surprise) – Ben Bolker May 07 '20 at 23:34
0

In R, it is very easy to do with melt(). If your data is in csv, use read_csv and use melt() and then write it back to CSV if needed.

Shan R
  • 521
  • 4
  • 8
  • yes. Note that `melt` is in the `reshape2` package (and gives two columns, but you can discard the first). Probably slower than @akrun's solution (but might not matter). – Ben Bolker May 07 '20 at 23:36