I have the following lists:
l1 <- list(a = 3, b = 4, c = 8, d = 1)
l2 <- list(a = 3, b = 2, c = 5, d = 1, f = 4, g = 13)
How can merge both lists by summing the items in both lists based on their names, as:
l1 + l2 = list(a=6, b=6, c=13, d=2, f=4, g=13)
You could approach it with dplyr
as follows:
l1 <- list(a = 3, b = 4, c = 8, d = 1)
l2 <- list(a = 3, b = 2, c = 5, d = 1, f = 4, g = 13)
library(dplyr)
bind_rows(lapply(list(l1, l2), as.data.frame)) %>%
colSums(na.rm=TRUE) %>%
as.list()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With