I'm working in R and have two lists of dataframes. I need to add a column to the dataframes within list1, filled with values taken from corresponding dataframes within list2. I think I need Map within Map, or lapply within Map, but I have only ever used Map and lapply on their own, or a lapply within a lapply so I am having trouble getting the correct code sorted. The below lists are similar to what I am working with.
dfa<-data.frame(tom=sample(seq(as.Date("2020-01-01"),as.Date("2020-03-01"), 1), 20), id=seq(1:20))
dfb<-data.frame(tom=sample(seq(as.Date("2020-01-01"),as.Date("2020-02-01"), 1), 32), id=seq(1:32))
list1<-list(dfa, dfb)
df1<-data.frame(date=seq(as.Date("2020-01-01"), as.Date("2020-03-01"), 1),num=sample(1:200, 61))
df2<-data.frame(date=seq(as.Date("2020-01-01"), as.Date("2020-03-01"), 1),num=sample(4:200, 61))
list2<-list(df1, df2)
The code that I have tried might be close or totally off mark. I am trying to add the new column "d1" to all dataframes in list1. The values in d1 should be pulled from variable "num" in the dataframes of list2, where the dates match to the dates from dataframes in list1.
list3<-Map(function(x, y){
Map(function(v, w){
v<-cbind(v, d1=w[w$date==v, w$num])}, x$tom, y)},list1, list2)
This just returns an error regarding use of atomic vectors: "Error in w$date : $ operator is invalid for atomic vectors"
But if I change it to indexing...
list3<-Map(function(x, y){
Map(function(v, w){
v<-cbind(v, d1=w[w[,1]==v, w[,2]])}, x$tom, y)}, list1, list2)
It is not happy either: "Error in [.default(w, , 1) : incorrect number of dimensions"
By this point I've sort of lost track of what I'm trying to refer to in the innermost function and how to refer to it properly. Is this even getting close to what I want?