我想通过id拆分mylist并有可能将产品值加起来。我遇到过lapply,但遗憾的是没有找到任何解决方案使用lapply with aggregate-embedded。
list1 = list(id=1 , product=5)
list2 = list(id=1 , product=6)
list2 = list(id=2 , product=5)
list3 = list(id=1 , product=4)
list4 = list(id=3 , product=9)
mylist <- list(list1, list2, list3, list4)
答案 0 :(得分:1)
我们可transpose
list
,绑定行,然后通过&#39; id&#39;
library(purrr)
library(dplyr)
mylist %>%
transpose %>%
map_df(unlist) %>%
group_by(id) %>%
summarise(product = sum(product))
或上述的另一种变体是
mylist %>%
transpose %>%
as_tibble %>%
unnest %>%
group_by(id) %>%
summarise(product = sum(product))
答案 1 :(得分:0)
库(data.table)
newlist&lt; - lapply(mylist,as.data.table); dt&lt; - rbindlist(newlist); data.table :: dcast(dt,“id~。”,value.var =“product”,fun = sum)