I would like to perform a left_join between elements of two lists, conditional on the fact that the lista share the same name (I cannot use a simple left_joinon the databases for memory reasons).
set.seed(0)
db_m1 <- data.frame(
y=rep(1,20),
id=sort(rep(paste0("id_",c(letters[1:4])),5)),
m1=rep(c(100,200),10),
x1=sample(LETTERS, 20, TRUE),
x2=sample(LETTERS, 20, TRUE)) %>%
split(f=list(.$y,.$m1))
set.seed(0)
db_m2 <- data.frame(y=rep(1,20),
m1=sample(seq(100,500,100),20,TRUE),
m2=sample(c(6:10),20,TRUE)) %>%
split(f=list(.$y,.$m1))
Desiderata:
$`1.100`
y id m1 x1 x2 m2
1 1 id_a 100 N O 10
2 1 id_a 100 N O 7
(...)
$`1.200`
y id m1 x1 x2 m2
7 1 id_a 200 Y U 6
8 1 id_a 200 Y U 9
(...)
CodePudding user response:
keys <- unique(c(names(candidates_0_s)))
setNames(lapply(keys, function(key){
result <- left_join(candidates_0_s[[key]],allies_0_s[[key]])
return(result)}
),keys)
CodePudding user response:
same idea, but in base R and working with your example:
common <- intersect(names(db_m1), names(db_m2))
merge_items <- function(item) merge(db_m1[[item]], db_m2[[item]], all.x = TRUE)
res <- lapply(common, merge_items)
output:
> lapply(res, head)
[[1]]
y m1 id x1 x2 m2
1 1 100 id_a N O 10
2 1 100 id_a N O 7
3 1 100 id_a N O 7
4 1 100 id_a N O 9
5 1 100 id_a N O 9
6 1 100 id_a N O 6
[[2]]
y m1 id x1 x2 m2
1 1 200 id_a Y U 6
2 1 200 id_a Y U 9
3 1 200 id_a Y U 9
4 1 200 id_a Y U 7
5 1 200 id_a G I 6
6 1 200 id_a G I 9
