# How to create a column of log(1+x) in data frames stored in R list?

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

To create a column of log(1+x) 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 log(1+x) 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=sample(1:50,25))
df2<-data.frame(x=sample(1:50,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
2  49
3  41
4  29
5  39
6  25
7   5
8  35
9  33
10 18
11 13
12 11
13 44
14 27
15 46
16  4
17 17
18 34
19 42
20  6
21 50
22 28
23  2
24 43
25 32
[[2]]
x
1  19
2  18
3  21
4  38
5  46
6  29
7  22
8  35
9  16
10  7
11 23
12 28
13 15
14 11
15 42
16  2
17 43
18 31
19 34
20  1
21 47
22 14
23 24
24 12
25  3

Create a column of log(1+x) in data frames stored in the list

Using lapply function to create a column of log(1+x) in data frames df1 and df2 stored in the list called List as shown below −

df1<-data.frame(x=sample(1:50,25))
df2<-data.frame(x=sample(1:50,25))
List<-list(df1,df2)
lapply(List,function(x) {
+ x$NaturalLog1PlusX<-log1p(x$x)
+ return(x)
+ })

## Output

[[1]]
x  NaturalLog1PlusX
1  1   0.6931472
2 49   3.9120230
3 41   3.7376696
4 29   3.4011974
5 39   3.6888795
6 25   3.2580965
7 5    1.7917595
8 35   3.5835189
9 33   3.5263605
10 18  2.9444390
11 13  2.6390573
12 11  2.4849066
13 44  3.8066625
14 27  3.3322045
15 46  3.8501476
16 4   1.6094379
17 17  2.8903718
18 34  3.5553481
19 42  3.7612001
20 6   1.9459101
21 50  3.9318256
22 28  3.3672958
23 2   1.0986123
24 43  3.7841896
25 32  3.4965076
[[2]]
x    NaturalLog1PlusX
1  19   2.9957323
2  18   2.9444390
3  21   3.0910425
4  38   3.6635616
5  46   3.8501476
6  29   3.4011974
7  22   3.1354942
8  35   3.5835189
9  16   2.8332133
10  7   2.0794415
11 23   3.1780538
12 28   3.3672958
13 15   2.7725887
14 11   2.4849066
15 42   3.7612001
16  2   1.0986123
17 43   3.7841896
18 31   3.4657359
19 34   3.5553481
20  1   0.6931472
21 47   3.8712010
22 14   2.7080502
23 24   3.2188758
24 12   2.5649494
25  3   1.3862944
Updated on 08-Nov-2021 11:13:15