How to convert columns of an R data frame into rows?


If the row values are incorrectly recorded into columns then we might want to convert the columns into rows. Thus, to convert columns of an R data frame into rows we can use transpose function t. For example, if we have a data frame df with five columns and five rows then we can convert the columns of the df into rows by using as.data.frame(t(df)).

Example

 Live Demo

set.seed(4)
x1<-rnorm(5,10,2)
x2<-letters[1:5]
x3<-1:5
df1<-data.frame(x1,x2,x3)
df1

Output

      x1    x2 x3
1 10.433510 a  1
2 8.915015  b  2
3 11.782289 c  3
4 11.191961 d  4
5 13.271236 e  5

Converting columns of df1 to rows −

Example

df1_new<-as.data.frame(t(df1))
df1_new

Output

       V1        V2       V3       V4       V5
x1 10.433510 8.915015 11.782289 11.191961 13.271236
x2      a         b       c         d        e
x3      1         2       3         4        5

Example

 Live Demo

a<-sample(0:1,20,replace=TRUE)
b<-sample(0:5,20,replace=TRUE)
c<-sample(1:10,20,replace=TRUE)
d<-sample(6:10,20,replace=TRUE)
df2<-data.frame(a,b,c,d)
df2

Output

   a b c d
1  0 3 5 10
2  1 4 9 10
3  1 4 2  9
4  1 5 10 7
5  1 4 4  6
6  1 0 8  6
7  1 5 10 9
8  0 2 2  6
9  1 5 10 9
10 0 4 6  9
11 0 3 10 9
12 0 0 6 10
13 0 3 1 10
14 0 5 4  7
15 1 2 2 10
16 1 4 4 10
17 0 3 3  8
18 0 3 1  7
19 1 5 7  9
20 1 5 7  7

Converting columns of df2 to rows −

Example

df2_new<-as.data.frame(t(df2))
df2_new

Output

  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
a 0   1 1   1 1   1  1  0 1    0   0   0  0   0    1   1   0   0   1 1
b 3   4 4   5 4   0  5  2 5    4   3   0  3   5    2   4   3   3   5 5
c 5   9 2   10 4  8  10 2 10   6   10  6  1   4    2   4   3   1   7 7
d 10  10 9  7  6  6  9  6 9    9   9  10  10  7   10  10   8   7   9 7

Example

 Live Demo

y1<-LETTERS[1:20]
y2<-letters[1:20]
y3<-sample(LETTERS[1:4],20,replace=TRUE)
df3<-data.frame(y1,y2,y3)
df3

Output

 y1 y2 y3
1  A a A
2  B b A
3  C c D
4  D d C
5  E e C
6  F f B
7  G g A
8  H h C
9  I i B
10 J j A
11 K k A
12 L l C
13 M m C
14 N n A
15 O o A
16 P p C
17 Q q D
18 R r D
19 S s D
20 T t D

Converting columns of df3 to rows −

Example

df3_new<-as.data.frame(t(df3))
df3_new

Output

  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
y1 A B   C D   E F   G  H I   J   K   L    M   N   O   P  Q    R  S   T
y2 a b   c d   e f   g  h i   j   k   l    m   n   o   p  q    r  s   t
y3 A A   D C   C B   A  C B   A   A   C    C   A   A   C  D    D  D   D

Example

 Live Demo

z1<-rpois(20,10)
z2<-rpois(20,5)
z3<-rpois(20,8)
z4<-rpois(20,4)
df4<-data.frame(z1,z2,z3,z4)
df4

Output

   z1 z2 z3 z4
1  10 11 13 2
2  8  3   9 2
3  6  4  10 3
4 16  6  10 4
5  9  7   5 8
6  4  5   5 3
7  7  4   3 2
8  8  2   9 3
9  12 6   9 4
10 15 7   9 3
11 8  5  10 2
12 20 4   6 4
13  5 4  10 8
14 10 2  8  1
15 20 5  9  3
16  5 5  8  5
17 13 5   8 6
18 7 1   14 3
19 12 2   9 6
20 9 8    11 2

Converting columns of df4 to rows −

Example

df4_new<-as.data.frame(t(df4))
df4_new

Output

   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
z1 10 8   6 16  9 4  7   8 12 15   8   20   5  10 20    5  13  7   12  9
z2 11 3   4 6   7 5  4   2 6  7    5   4    4   2  5    5  5   1   2   8
z3 13 9  10 10  5 5  3   9 9  9    10  6    10  8  9    8  8   14  9   11
z4 2 2    3 4   8 3  2   3 4  3    2   4    8   1  3    5  6   3   6    2

Updated on: 17-Oct-2020

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements