# How to create a column of log with base 2 in data frames stored in R list?

To create a column of log with base 2 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 with base 2 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:100,25))
df2<-data.frame(x=sample(1:100,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   5
2  70
3  75
4  19
5  41
6  52
7  55
8  90
9   1
10 81
11 18
12 65
13 10
14 21
15  9
16 69
17 94
18 63
19 28
20 51
21 62
22 36
23 98
24 39
25 50
[[2]]
x
1  33
2  58
3  59
4  12
5   3
6  60
7  11
8  50
9  10
10 16
11 76
12 25
13 88
14 92
15 46
16 57
17  6
18 91
19 71
20 35
21 32
22 65
23 19
24 53
25 82

Create a column of log with base 2 in data frames stored in the list

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

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

## Output

[[1]]
x LogBase2
1   5 2.321928
2  70 6.129283
3  75 6.228819
4  19 4.247928
5  41 5.357552
6  52 5.700440
7  55 5.781360
8  90 6.491853
9   1 0.000000
10 81 6.339850
11 18 4.169925
12 65 6.022368
13 10 3.321928
14 21 4.392317
15  9 3.169925
16 69 6.108524
17 94 6.554589
18 63 5.977280
19 28 4.807355
20 51 5.672425
21 62 5.954196
22 36 5.169925
23 98 6.614710
24 39 5.285402
25 50 5.643856
[[2]]
x LogBase2
1  33 5.044394
2  58 5.857981
3  59 5.882643
4  12 3.584963
5   3 1.584963
6  60 5.906891
7  11 3.459432
8  50 5.643856
9  10 3.321928
10 16 4.000000
11 76 6.247928
12 25 4.643856
13 88 6.459432
14 92 6.523562
15 46 5.523562
16 57 5.832890
17  6 2.584963
18 91 6.507795
19 71 6.149747
20 35 5.129283
21 32 5.000000
22 65 6.022368
23 19 4.247928
24 53 5.727920
25 82 6.357552