How to take transpose of vectors stored in an R list?

R ProgrammingServer Side ProgrammingProgramming

We can take the transpose of vectors stored in an R list by using Map function with do.call. For example, if we have a list called LIST that contains say ten vectors then we can transpose these vectors by using the command given below −

do.call(Map,c(f=c,LIST))

Check out the below examples to understand how it works.

Example 1

To take transpose of vectors stored in an R list, use the command given below −

List1<-list(x1=rpois(20,5),x2=rpois(20,1),x3=rpois(20,10))
List1

If you execute the above given command, it generates the following output −

$x1
[1] 4 10 1 4 2 7 9 8 3 9 2 1 2 4 4 7 4 0 4 2

$x2
[1] 2 0 1 1 2 0 0 2 1 1 1 1 0 2 1 1 3 0 1 1

$x3
[1] 14 8 8 10 6 6 12 8 17 5 13 8 8 15 11 16 11 10 13 13

To take transpose of vectors stored in an R list, add the following code to the above command −

List1<-list(x1=rpois(20,5),x2=rpois(20,1),x3=rpois(20,10))
do.call(Map,c(f=c,List1))

Output

If you execute all the above given commands as a single program, it generates the following output −

[[1]]
x1 x2 x3
4  2  14

[[2]]
x1 x2 x3
10 0  8

[[3]]
x1 x2 x3
 1 1  8

[[4]]
x1 x2 x3
 4 1  10

[[5]]
x1 x2 x3
 2 2  6

[[6]]
x1 x2 x3
 7 0  6

[[7]]
x1 x2 x3
 9 0  12

[[8]]
x1 x2 x3
 8 2  8

[[9]]
x1 x2 x3
 3 1  17

[[10]]
x1 x2 x3
 9 1  5

[[11]]
x1 x2 x3
 2 1  13

[[12]]
x1 x2 x3
 1 1  8

[[13]]
x1 x2 x3
 2 0  8

[[14]]
x1 x2 x3
 4 2  15

[[15]]
x1 x2 x3
 4 1  11

[[16]]
x1 x2 x3
 7 1  16

[[17]]
x1 x2 x3
 4 3  11

[[18]]
x1 x2 x3
 0 0  10

[[19]]
x1 x2 x3
 4 1  13

[[20]]
x1 x2 x3
 2 1  13

Example 2

To take transpose of vectors stored in an R list, use the command given below −

List2<-list(y1=rnorm(20),y2=rnorm(20))
List2

If you execute the above given command, it generates the following output −

$y1
[1] 0.05917548 1.02861792 0.18393866 0.15641054 0.48371132 -0.97462169
[7] -0.08079743 -0.46323450 1.93425942 -1.01678126 0.46897927 -0.17711315
[13] 0.22473314 -0.65702542 0.69822243 0.37815176 0.41582381 -0.18821946
[19] 1.43258693 0.22601076

$y2
[1] -1.3239608 -0.2197707 0.1915578 0.5647687 0.6934978 -0.6755007
[7] 0.9405100 -1.0617483 -0.7765754 -0.4334703 -0.5402686 0.8850059
[13] 1.5196022 1.0401178 1.5100634 0.7502288 0.3609667 2.5214306
[19] -0.6588718 0.7632775

To take transpose of vectors stored in an R list, add the following code to the above command −

List2<-list(y1=rnorm(20),y2=rnorm(20))
do.call(Map,c(f=c,List2))

Output

If you execute all the above given snippets as a single program, it generates the following output: −

[[1]]
y1            y2
0.05917548 -1.32396077

[[2]]
    y1        y2
1.0286179 -0.2197707

[[3]]
    y1       y2
0.1839387 0.1915578

[[4]]
     y1      y2
0.1564105 0.5647687

[[5]]
    y1       y2
0.4837113 0.6934978

[[6]]
    y1         y2
-0.9746217 -0.6755007

[[7]]
      y1      y2
-0.08079743 0.94051002

[[8]]
   y1          y2
-0.4632345 -1.0617483

[[9]]
   y1        y2
1.9342594 -0.7765754

[[10]]
    y1      y2
-1.0167813 -0.4334703

[[11]]
   y1       y2
0.4689793 -0.5402686

[[12]]
    y1       y2
-0.1771132 0.8850059

[[13]]
   y1       y2
0.2247331 1.5196022

[[14]]
    y1       y2
-0.6570254 1.0401178

[[15]]
    y1      y2
0.6982224 1.5100634

[[16]]
    y1     y2
0.3781518 0.7502288

[[17]]
    y1      y2
0.4158238 0.3609667

[[18]]
    y1         y2
-0.1882195 2.5214306

[[19]]
    y1       y2
1.4325869 -0.6588718

[[20]]
    y1      y2
0.2260108 0.7632775
raja
Updated on 10-Nov-2021 13:27:50

Advertisements