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


To create a column of rounded values 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 rounded valuesin 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=rnorm(25))
df2<-data.frame(x=rnorm(25))
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
1  -1.60910333
2  -1.03729474
3  -1.40448389
4  -0.24728310
5   1.30467124
6  -1.13207279
7   0.82561709
8   0.16409103
9  -0.78255736
10  0.18421525
11  0.13996475
12 -0.83319702
13 -0.24966421
14  0.97679457
15  1.03381662
16 -0.78863545
17  0.19427251
18 -1.55102854
19  0.51994225
20 -0.75731012
21 -0.08022442
22 -0.62130337
23 -0.82960514
24  1.00996406
25 -1.78565706
[[2]]
      x
1  -1.61930147
2  -1.34536134
3   0.50974049
4   0.72663429
5   1.36639765
6  -0.32173730
7  -0.70752541
8  -0.48263801
9   1.39796383
10  0.02785204
11 -1.24980801
12 -1.60321222
13 -0.65432900
14  0.35995917
15  0.00399240
16  0.18852787
17 -0.88659596
18 -1.56006812
19  1.44433948
20 -1.40241760
21 -0.59547084
22 -0.03665240
23  0.27673451
24  0.53268773
25 -1.52717234

Create a column of rounded values

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

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

Output

[[1]]
       x       Rounded
1  -1.60910333 -1.61
2  -1.03729474 -1.04
3  -1.40448389 -1.40
4  -0.24728310 -0.25
5   1.30467124  1.30
6  -1.13207279 -1.13
7   0.82561709  0.83
8   0.16409103  0.16
9  -0.78255736 -0.78
10  0.18421525  0.18
11  0.13996475  0.14
12 -0.83319702 -0.83
13 -0.24966421 -0.25
14  0.97679457  0.98
15  1.03381662  1.03
16 -0.78863545 -0.79
17  0.19427251  0.19
18 -1.55102854 -1.55
19  0.51994225  0.52
20 -0.75731012 -0.76
21 -0.08022442 -0.08
22 -0.62130337 -0.62
23 -0.82960514 -0.83
24  1.00996406  1.01
25 -1.78565706 -1.79
[[2]]
       x       Rounded
1  -1.61930147 -1.62
2  -1.34536134 -1.35
3   0.50974049  0.51
4   0.72663429  0.73
5   1.36639765  1.37
6  -0.32173730 -0.32
7  -0.70752541 -0.71
8  -0.48263801 -0.48
9   1.39796383  1.40
10  0.02785204  0.03
11 -1.24980801 -1.25
12 -1.60321222 -1.60
13 -0.65432900 -0.65
14  0.35995917  0.36
15  0.00399240  0.00
16  0.18852787  0.19
17 -0.88659596 -0.89
18 -1.56006812 -1.56
19  1.44433948  1.44
20 -1.40241760 -1.40
21 -0.59547084 -0.60
22 -0.03665240 -0.04
23  0.27673451  0.28
24  0.53268773  0.53
25 -1.52717234 -1.53

Updated on: 10-Nov-2021

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements