R: apply function to two consecutive dataframes in a list of dfs

Solution 1:

Here is a base R solution using lapply for iteration:

df_list_match <- function(df1, df2){
  new <- merge(df1, df2, by="name", all.y=T)
  new$combined <- (new$column1.x + new$column2.x)/(new$column2.y)
  new <- new[!is.na(new$combined), c(1,4,5,6)]
  names(new) <- c("name", "column1", "column2", "combined")
  return(new)
}

result <- lapply(2:length(df_list), function(x) {df_list_match(df_list[[x-1]],df_list[[x]]) })

result
[[1]]
   name column1 column2 combined
1  mark       7       1 5.000000
2 peter       9       3 2.333333

[[2]]
   name column1 column2 combined
1  liam       5       5      2.0
2 peter       8       8      1.5

[[3]]
   name column1 column2 combined
1 felix       6       4        1
2  liam       6       2        5
    

If you want to have all original entries from the data.frame (display NA if an entry in dfy is not in dfx) you can just delete !is.na(new$combined) in the function.