rdataframevertical-alignmenthorizontal-line

Convert dataframe from vertical to horizontal


I already checked many questions and I don't seem to find the suitable answer.

I have this df

df = data.frame(x = 1:10,y=11:20)

the output

    x  y
1   1 11
2   2 12
3   3 13
4   4 14
5   5 15
6   6 16
7   7 17
8   8 18
9   9 19
10 10 20

I just wish the output to be:

  1 2 3 4 5 6 7 8 9 10
x 1 2 3 4 5 6 7 8 9 10
y 11 12 13 14 15 16 17 18 19 20

thanks


Solution

  • Try t() like below

    > data.frame(t(df), check.names = FALSE)
       1  2  3  4  5  6  7  8  9 10
    x  1  2  3  4  5  6  7  8  9 10
    y 11 12 13 14 15 16 17 18 19 20