Here's a simplified example of my data:
I have a list of dataframes
set.seed(1)
data1 <- data.frame(A = sample(1:10))
data2 <- data.frame(A = sample(1:10))
data3 <- data.frame(A = sample(1:10))
data4 <- data.frame(A = sample(1:10))
list1 <- list(data1, data2, data3, data4)
And a dataframe containing the same number of values as there are dataframes in list1
data5 <- data.frame(B = c(10, 20, 30, 40))
I would like to create a new column C
in each of the dataframes within list1
where:
C = A * (B/nrow(A))
with the value for B
coming from data5
, so that B = 10
for the first dataframe in list1
(i.e. data1
), and B = 20
for the second dataframe data2
and so on.
From what I've read, mapply
is probably the solution, but I'm struggling to work out how to specify a single value of B
across all rows in each of the dataframes in list1
.
Any suggestions would be hugely appreciated.