How to convert rows in an R data frame to list?

R ProgrammingServer Side ProgrammingProgramming

Sometimes each row needs to be treated differently, therefore, we might want to convert those rows into list. This will help us to perform the operations on our row elements separately. To convert the rows into list, we can use split function by defining the number of rows in the data frame.

Consider the below data frame −

Example

 Live Demo

set.seed(101)
x1<-rnorm(20,5,1)
x2<-rnorm(20,5,2)
x3<-rnorm(20,5,1.5)
x4<-rnorm(20,3,0.096)
df1<-data.frame(x1,x2,x3,x4)
df1

Output

     x1       x2      x3       x4
1 4.673964 4.672489 5.723688 2.975059
2 5.552462 6.417044 6.137321 2.864527
3 4.325056 4.464039 1.521009 2.938430
4 5.214359 2.072156 4.310743 3.010796
5 5.310769 6.488872 3.341924 3.040570
6 6.173966 2.179220 5.604392 3.037136
7 5.618790 5.934135 5.853402 2.933971
8 4.887266 4.761360 3.940875 3.014295
9 5.917028 5.934478 4.564864 2.994466
10 4.776741 5.996271 2.774183 2.992817
11 5.526448 6.789874 3.274617 3.144950
12 4.205156 5.558304 4.588293 3.155514
13 6.427756 7.015732 5.866852 3.110703
14 3.533180 0.853787 2.904646 2.992550
15 4.763317 7.379707 6.123587 2.825382
16 4.806662 3.551252 3.423220 2.900405
17 4.150245 5.335968 5.248071 3.029039
18 5.058465 6.840670 6.694714 2.877317
19 4.182330 1.656790 6.760584 3.013281
20 2.949692 5.896938 4.358205 2.995106

Converting rows of df1 into a list −

df1_row_list<-split(df1,1:nrow(df1))

df1_row_list

$`1`

x1 x2 x3 x4
1 4.673964 4.672489 5.723688 2.975059

$`2`

x1 x2 x3 x4
2 5.552462 6.417044 6.137321 2.864527

$`3`

x1 x2 x3 x4
3 4.325056 4.464039 1.521009 2.93843

$`4`

     x1       x2 x3 x4
4 5.214359 2.072156 4.310743 3.010796

$`5`

    x1        x2       x3     x4
5 5.310769 6.488872 3.341924 3.04057

$`6`

x1 x2 x3 x4
6 6.173966 2.17922 5.604392 3.037136

$`7`

x1 x2 x3 x4
7 5.61879 5.934135 5.853402 2.933971

$`8`

x1 x2 x3 x4
8 4.887266 4.76136 3.940875 3.014295

$`9`

x1 x2 x3 x4
9 5.917028 5.934478 4.564864 2.994466

$`10`

x1 x2 x3 x4
10 4.776741 5.996271 2.774183 2.992817

$`11`

x1 x2 x3 x4
11 5.526448 6.789874 3.274617 3.14495

$`12`

x1 x2 x3 x4
12 4.205156 5.558304 4.588293 3.155514

$`13`

x1 x2 x3 x4
13 6.427756 7.015732 5.866852 3.110703

$`14`

x1 x2 x3 x4
14 3.53318 0.853787 2.904646 2.99255

$`15`

x1 x2 x3 x4
15 4.763317 7.379707 6.123587 2.825382

$`16`

x1 x2 x3 x4
16 4.806662 3.551252 3.42322 2.900405

$`17`

x1 x2 x3 x4
17 4.150245 5.335968 5.248071 3.029039

$`18`

x1 x2 x3 x4
18 5.058465 6.84067 6.694714 2.877317

$`19`

x1 x2 x3 x4
19 4.18233 1.65679 6.760584 3.013281

$`20`

x1 x2 x3 x4
20 2.949692 5.896938 4.358205 2.995106

is.list(df1_row_list) [1] TRUE

Let’s have a look at another example −

Example

 Live Demo

y1<-LETTERS[1:20]
y2<-1:20
y3<-sample(0:9,20,replace=TRUE)
y4<-rpois(20,3)
y5<-rexp(20,3)
df2<-data.frame(y1,y2,y3,y4,y5)
df2

Output

 y1  y2  y3  y4  y5
1 A 1 6 1 0.12535201
2 B 2 1 4 0.04849127
3 C 3 7 4 0.10894953
4 D 4 0 4 0.32620261
5 E 5 3 7 0.04711375
6 F 6 5 2 0.10263975
7 G 7 3 1 0.35080088
8 H 8 2 4 0.25617221
9 I 9 0 0 0.31124956
10 J 10 0 5 0.07771391
11 K 11 7 2 0.02583599
12 L 12 2 2 0.06699178
13 M 13 3 3 0.04767600
14 N 14 1 4 0.11942077
15 O 15 6 4 0.62873345
16 P 16 3 6 0.08066371
17 Q 17 3 3 0.58563662
18 R 18 9 0 0.00548393
19 S 19 0 2 0.01171107
20 T 20 0 1 0.10728116

Converting rows of df2 into list −

df2_row_list<-split(df2,1:nrow(df2))

df2_row_list

$`1`

y1 y2 y3 y4 y5
1 A 1 4 2 0.02331396

$`2`

y1 y2 y3 y4 y5
2 B 2 6 2 0.2343971

$`3`

y1 y2 y3 y4 y5
3 C 3 1 5 0.022928

$`4`

y1 y2 y3 y4 y5
4 D 4 3 2 0.009332871

$`5`

y1 y2 y3 y4 y5
5 E 5 3 4 0.3321631

$`6`

y1 y2 y3 y4 y5
6 F 6 8 5 0.6256276

$`7`

y1 y2 y3 y4 y5
7 G 7 8 2 0.03358184

$`8`

y1 y2 y3 y4 y5
8 H 8 2 3 0.2089223

$`9`

y1 y2 y3 y4 y5
9 I 9 6 3 0.5231238

$`10`

y1 y2 y3 y4 y5
10 J 10 7 3 0.01456478

$`11`

y1 y2 y3 y4 y5
11 K 11 2 2 0.670605

$`12`

y1 y2 y3 y4 y5
12 L 12 1 2 0.1021066

$`13`

y1 y2 y3 y4 y5
13 M 13 5 7 0.2673457

$`14`

y1 y2 y3 y4 y5
14 N 14 0 1 0.442615

$`15`

y1 y2 y3 y4 y5
15 O 15 3 5 0.2244831

$`16`

y1 y2 y3 y4 y5
16 P 16 7 2 0.2897614

$`17`

y1 y2 y3 y4 y5
17 Q 17 2 4 0.4671283

$`18`

y1 y2 y3 y4 y5
18 R 18 7 3 0.274696

$`19`

y1 y2 y3 y4 y5
19 S 19 0 2 0.1120801

$`20`

y1 y2 y3 y4 y5
20 T 20 8 5 0.2727685

is.list(df2_row_list) [1] TRUE

raja
Published on 10-Oct-2020 12:16:53
Advertisements