[英]R Troubleshooting: Sum values of one column in a data frame based on values in another column of the data frame
[英]How to sum values from one data frame into another based on a column of lists in R
我被困在这里,相信我把这个问题复杂化了......
我试图通过在由“,”分隔的县列表中检测到分类变量“县”来对“数字”求和。
有没有办法在 base 和/或 dplyr 中做到这一点? 我尝试了 str_detect 和 filter 的一些变体,但似乎无法弄清楚这一点。
在数据帧 d1-d3 处查看以下代码:
d1 <- data.frame(County = c("a", "b", "c", "d") ,
Number = c(1000, 2000, 3000, 4000))
d1
County Number
1 a 1000
2 b 2000
3 c 3000
4 d 4000
d2 <- data.frame(County = c("a, b", "b, c", "c", "d, a", "a, c, d, b"))
d2
County
1 a, b
2 b, c
3 c
4 d, a
5 a, c, d, b
d3 <-
d3
County Total
1 a, b 3000
2 b, c 5000
3 c 3000
4 d, a 5000
5 a, c, d, b 10000
做这两个
d2$Total <- Map(f= function(x) sum(d1$Number[match(x, d1$County)]), strsplit(d2$County, ", "))
> d2
County Total
1 a, b 3000
2 b, c 5000
3 c 3000
4 d, a 5000
5 a, c, d, b 10000
library(tidyverse)
d2 %>% mutate(Total = map(str_split(County, ", "), ~ sum(d1$Number[match(.x, d1$County)])))
County Total
1 a, b 3000
2 b, c 5000
3 c 3000
4 d, a 5000
5 a, c, d, b 10000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.