多个条件使用 df 中的元素匹配查找表中的列名以合并 3 个数据帧

鸟脑

我有三个大数据框,我想根据几个标准将一些元素从一个元素附加到另一个元素。我在 Stack Overflow 中查找了类似的问题,但它们似乎不适用于我的数据帧格式(或者我不够熟练,无法正确调整)。

需要发生的是:

  1. 在 maindf1 中按性别过滤
  2. 在maledflookup的rowname(第一列)的maindf1中搜索相同的ZCTA值
  3. 同时在maledflookup的列名中从maindf1的一行中搜索正确的年龄层
  4. 使用匹配的 ZCTA 向 maindf1 行添加一列新数据,该 ZCTA 具有从maledflookup 中获取的该性别和年龄层的人口普查人口值
  5. 重复女性dflookup
  6. 最终结果是 maindf1 对按性别、ZCTA 和年龄层匹配的每一行都有一个 censuspop 值

maindf1 是原始数据,其中每一行都是一个人,列是调查响应或收集的个人数据

我必须使用的人口普查网站上的查找表采用了奇怪的格式,因此对我来说解决其中一个问题的最简单的解决方案是首先按性别分隔查找表。

我在编写成功的代码方面没有运气,因为我还没有在 R 中编码的经验。我尝试了一些 for & if 循环,但未能为此任务调整模糊连接代码。我感谢您的帮助!

示例数据:

ZCTA<- c("12345", "NA", "NA", "44444", "99999", "11111" )
sex <- c("female", "male", "male", "male", "female", "male")
agegrp <- c("pop_0to4", "pop_70to74", "pop_25to29", "pop_70to74","pop_70to74","pop_25to29")
maindf1 <- data.frame(ZCTA, sex, agegrp)

ZCTA<- c("12345", "23456", "12225", "44444", "99999", "11111" )
pop_0to4 <- c("2000", "1300", "900", "737", "289", "120")
pop_70to74 <- c("25", "222", "52", "160", "100", "80")
pop_25to29 <- c("3000", "2500", "102", "1777", "3390", "2450")
maledflookup<- data.frame(ZCTA, pop_0to4, pop_25to29, pop_70to74)

ZCTA<- c("12345", "23456", "12225", "44444", "99999", "11111" )
pop_0to4 <- c("1111", "2333", "999", "888", "222", "122")
pop_70to74 <- c("18", "333", "66", "300", "90", "99")
pop_25to29 <- c("3333", "2555", "111", "2777", "3311", "2121")
femaledflookup <- data.frame(ZCTA, pop_0to4, pop_25to29, pop_70to74)

数据和查找表看起来像(2000 行):

#maindf1
#ZCTA   #sex    #agegrp
12345   female  pop_0to4        
NA      male    pop_70to74      
NA      male    pop_25to29      
44444   male    pop_70to74      
99999   female  pop_70to74      
11111   male    pop_25to29

#maledflookup
#ZCTA   #pop_0to4  #pop_25to29  #pop_70to74
12345   2000       3000         25  
23456   1300       2500         222 
12225   900        102          52  
44444   737        1777         160 
99999   289        3390         100 
11111   120        2450         80

#femaledflookup
#ZCTA   #pop_0to4  #pop_25to29  #pop_70to74
12345   1111        3333         18 
23456   2333        2555        333 
12225   999         111          66 
44444   888         2777        300 
99999   222         3311         90 
11111   122         2121         99

期望的结果:

#maindf1
#ZCTA   #sex    #agegrp       #censuspop
12345   female  pop_0to4        1111        
NA      male    pop_70to74      NA
NA      male    pop_25to29      NA
44444   male    pop_70to74      160 
99999   female  pop_70to74      90  
11111   male    pop_25to29      2450            
petrucci4prez

使用left_jointidyverse 和格式正确的查找表:

library(tidyverse)

.maledflookup <- maledflookup %>%
  gather(-ZCTA, key = agegrp, value = censuspop) %>%
  mutate(sex = "male")

.femaledflookup <- femaledflookup %>%
  gather(-ZCTA, key = agegrp, value = censuspop) %>%
  mutate(sex = "female")

.lookup <- bind_rows(.maledflookup, .femaledflookup)

left_join(maindf1, .lookup, by = c("sex", "ZCTA", "agegrp"))
  1. 使用数据透视男性的数据gather,以获得与列的数据帧ZCTAagegrpcensuspop还为性别添加一个新列。
  2. 重复女性数据
  3. 使用连接男性和女性数据 bind_rows
  4. 加入查找表使用ZCTAagegrpsex

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用python中的索引位置在单个df中附加多个数据帧

我有一个很长的数据帧列表,并希望使用 for 循环将每个数据帧转换为 numpy 数组 X1、X2、X3 给定的 Pandas 数据帧 df1、df2、df3 在 python 中

在 R 中合并多个数据帧

使用2个数据帧的IP列和日期范围,用df2中的数据填充df1数据帧

使用df2日期时间中df1的“小时”和“分钟”上的条件合并2个数据帧

如何根据另一个数据帧 Python3 的条件删除数据帧中的列

Python将多个数据帧中的列合并到单个数据帧中

从原始数据帧中获取多个数据帧

根据多个条件合并两个数据帧

spark scala 数据帧合并多个数据帧

基于多个条件匹配两个数据帧的行

多个数据帧上的熊猫元素条件操作

在列表中连接多个数据帧

使用循环从多个数据帧的列中创建单个数据帧

加入/合并多个数据帧

R - 替换多个数据帧中列名中的模式

使用合并功能在两个数据帧上运行多个条件以创建对

根据另一个数据帧中的多个条件过滤数据帧

在以多个数据帧列为条件的数据帧中添加一列

如何从R中具有多个条件的一个数据帧创建多个数据帧

使用多个“ by”合并/合并两个数据帧?

根据索引合并两个数据帧(从数据透视表创建一个df)

避免在R中嵌套for循环,在多个条件下跨多个数据帧进行匹配

R 在列表中的多个数据帧中使用 grepl

使用循环在R中创建多个数据帧

在python中跨多个数据帧使用分类编码

在多个数据帧中按相同顺序查找行

使用pd.concat合并多个数据帧

比较两个数据帧,并根据匹配的列值从df中删除行