How to create a column of division in data frames stored in R list?

R ProgrammingServer Side ProgrammingProgramming

To create a column of division in data frames stored in R list, we can follow the below steps −

  • First of all, create a list of data frames.

  • Then, use lapply function to create a column of division in data frames stored in the list.

Example

Create the list of data frames

Using data.frame function to create data frames and list function to create the list of those data frames −

df1<-data.frame(x=round(rnorm(25),2),y=round(rnorm(25),2))
df2<-data.frame(x=round(rnorm(25),2),y=round(rnorm(25),2))
List<-list(df1,df2)
List

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

[[1]]
     x    y
1  -2.47  0.67
2  -0.55  1.44
3  -1.45  1.17
4   1.25 -0.58
5   0.72  0.51
6   0.29 -1.23
7   1.71  1.35
8   0.20  0.57
9  -1.30  0.56
10 -0.74 -1.68
11 -2.48 -0.17
12 -0.12  1.01
13  0.10 -0.56
14 -0.39  0.77
15  0.35  0.19
16 -1.41 -0.47
17 -1.00  1.20
18 -0.44  0.85
19  0.26  0.04
20 -0.86  0.09
21  1.07 -0.84
22  0.02  0.31
23 -0.57 -0.80
24  0.84 -0.12
25 -0.38 -1.51
[[2]]
     x    y
1   0.34  1.11
2   0.43 -1.32
3   0.63  0.20
4  -2.04  0.15
5  -0.99  1.15
6  -0.36 -0.59
7   0.15  0.69
8  -0.48 -0.62
9   0.77  0.33
10  0.58 -0.33
11  0.43  0.20
12 -1.16  0.27
13  0.47 -0.35
14 -0.39 -0.34
15  0.11  0.55
16 -0.90  0.02
17 -0.06 -1.58
18 -0.44  0.19
19 -0.15 -0.74
20  1.22 -0.81
21  0.77  0.42
22 -0.46  1.60
23  1.29 -0.20
24  0.58  1.07
25  0.29 -0.87

Create a column of division in data frames stored in the list

Using lapply function to create a column of division in data frames df1 and df2 stored in the list called List as shown below −

df1<-data.frame(x=round(rnorm(25),2),y=round(rnorm(25),2))
df2<-data.frame(x=round(rnorm(25),2),y=round(rnorm(25),2))
List<-list(df1,df2)
lapply(List,function(x) {
+ x$Division<-x$x/x$y
+ return(x)
+ })

Output

[[1]]
      x    y      Division
1  -2.47  0.67 -3.68656716
2  -0.55  1.44 -0.38194444
3  -1.45  1.17 -1.23931624
4   1.25 -0.58 -2.15517241
5   0.72  0.51  1.41176471
6   0.29 -1.23 -0.23577236
7   1.71  1.35  1.26666667
8   0.20  0.57  0.35087719
9  -1.30  0.56 -2.32142857
10 -0.74 -1.68  0.44047619
11 -2.48 -0.17  14.58823529
12 -0.12  1.01 -0.11881188
13  0.10 -0.56 -0.17857143
14 -0.39  0.77 -0.50649351
15  0.35  0.19  1.84210526
16 -1.41 -0.47  3.00000000
17 -1.00  1.20 -0.83333333
18 -0.44  0.85 -0.51764706
19  0.26  0.04  6.50000000
20 -0.86  0.09 -9.55555556
21  1.07 -0.84 -1.27380952
22  0.02  0.31  0.06451613
23 -0.57 -0.80  0.71250000
24  0.84 -0.12 -7.00000000
25 -0.38 -1.51  0.25165563
[[2]]
     x     y     Division
1   0.34  1.11  0.30630631
2   0.43 -1.32 -0.32575758
3   0.63  0.20  3.15000000
4  -2.04  0.15 -13.60000000
5  -0.99  1.15 -0.86086957
6  -0.36 -0.59  0.61016949
7   0.15  0.69  0.21739130
8  -0.48 -0.62  0.77419355
9   0.77  0.33  2.33333333
10  0.58 -0.33 -1.75757576
11  0.43  0.20  2.15000000
12 -1.16  0.27 -4.29629630
13  0.47 -0.35 -1.34285714
14 -0.39 -0.34  1.14705882
15  0.11  0.55  0.20000000
16 -0.90  0.02 -45.00000000
17 -0.06 -1.58  0.03797468
18 -0.44  0.19 -2.31578947
19 -0.15 -0.74  0.20270270
20  1.22 -0.81 -1.50617284
21  0.77  0.42  1.83333333
22 -0.46  1.60 -0.28750000
23  1.29 -0.20 -6.45000000
24  0.58  1.07  0.54205607
25  0.29 -0.87 -0.33333333
raja
Updated on 09-Nov-2021 07:43:19

Advertisements