Reputation: 403
I have a list of 25 data frames. My goal is to do a subtraction of one column from another in each list. For example:
a1 <- list(mtcars[1:5,], mtcars[6:10,])
I have to calculate drat
- wt
and make a new column to show to results. Like this:
[[1]]
mpg cyl disp hp drat wt qsec vs am gear carb Results
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 1.28
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 1.025
[[2]]
mpg cyl disp hp drat wt qsec vs am gear carb Results
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1 -0.70
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4 -0.36
I could not figure it out. Could someone help? Thanks!
Upvotes: 0
Views: 32
Reputation: 39154
a2
is a list with all the same data frame in a1
except that one column Results
is updated.
a1 <- list(mtcars[1:5,], mtcars[6:10,])
a2 <- lapply(a1, function(dt){
dt$Results <- dt$drat - dt$wt
return(dt)})
Upvotes: 1