简体   繁体   English

如何根据R中的一列列表将一个数据框中的值汇总到另一个数据框中

[英]How to sum values from one data frame into another based on a column of lists in R

I am stuck here and believe I am overcomplicating this problem...我被困在这里,相信我把这个问题复杂化了......

I am trying to sum a "Number" by a categorical variable "County" by detecting it in a list of counties separated by a ",".我试图通过在由“,”分隔的县列表中检测到分类变量“县”来对“数字”求和。

Is there a way to do this in base and/or dplyr?有没有办法在 base 和/或 dplyr 中做到这一点? I tried some variations of str_detect and filter, but can't seem to figure this out.我尝试了 str_detect 和 filter 的一些变体,但似乎无法弄清楚这一点。

Looking to the code below at data frames d1-d3:在数据帧 d1-d3 处查看以下代码:

  • d1 shows the "Number" associated with each "County" d1 显示与每个“县”相关联的“数字”
  • d2 shows each "County" list to sum d2 显示要求和的每个“县”列表
  • d3 should show the total sum of each "County" listed 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

Do Either of these做这两个

BaseR基础R

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

tidyverse整理宇宙

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.

相关问题 R 故障排除:根据数据框中另一列中的值对数据框中的一列的值求和 - R Troubleshooting: Sum values of one column in a data frame based on values in another column of the data frame 如何基于R中另一列的值从数据框中的一列提取值 - How to extract values from one column in a data frame based on values in another in R 如何根据一列中的值对数据进行装箱,并汇总R中另一列中的出现次数? - How to bin data based on values in one column, and sum occurrences from another column in R? R:从一个数据框中提取行,基于列名匹配另一个数据框中的值 - R: Extract Rows from One Data Frame, Based on Column Names Matching Values from Another Data Frame R:根据另一列操作一个数据框列的值 - R: Manipulate values of one data frame column based on another column R:根据列值将值从一个数据帧复制到另一个数据帧 - R: Copying values from one to another data frame based on column value R-如何根据数据帧中其他列的总和来最大化 - R - How to maximise based of sum of other column from data frame 对于R中的data.frame,根据来自另一个数据帧的值从一个数据帧提取数据 - For data.frame in R, pulling data from one data frame based on values from another data frame 如何基于一列的部分与另一数据框中的值的匹配来填充R中的列 - How to fill columns in R based on matching parts of one column to values in another data frame 基于R中另一列的数据帧中的列总和 - Sum column in data frame based on another column in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM