基于R中的两个匹配条件将值从一个数据帧添加到另一个数据帧
我在下面有两个数据帧: dput输出df1:基于R中的两个匹配条件将值从一个数据帧添加到另一个数据帧,r,dataframe,merge,R,Dataframe,Merge,我在下面有两个数据帧: dput输出df1: structure(list(Location = c("1100 2ND AVENUE", "1100 2ND AVENUE", "1100 2ND AVENUE", "1100 2ND AVENUE", "1100 2ND AVENUE", "1100 2ND AVENUE" ), `Ivend Name` = c("3 Mskt 1.92oz", "Almond Joy 1.61oz", "Aquafina 20oz", "BCanyonC
structure(list(Location = c("1100 2ND AVENUE", "1100 2ND AVENUE",
"1100 2ND AVENUE", "1100 2ND AVENUE", "1100 2ND AVENUE", "1100 2ND AVENUE"
), `Ivend Name` = c("3 Mskt 1.92oz", "Almond Joy 1.61oz", "Aquafina 20oz",
"BCanyonChptleAdzuk1.5oz", "BlkForest FrtSnk 2.25oz", "BluDimndSmkhseAlmd1.5oz"
), `Category Name` = c("Candy", "Candy", "Water", "Salty Snacks",
"Candy", "Nuts/Trailmix"), Calories = c(240, 220, 0, 215, 193,
260), Sugars = c("36", "20", "0", "2", "32", "2"), Month = structure(c(4L,
4L, 4L, 4L, 4L, 4L), .Label = c("Oct", "Nov", "Dec", "Jan", "Feb",
"Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep"), class = "factor"),
Products_available_per_machine = c(0, 0, 0, 0, 0, 0), Units_sold = c(0,
0, 0, 0, 0, 0), Total_Sales = c(0, 0, 0, 0, 0, 0), Spoils = c(0,
0, 0, 0, 0, 0), Building = c("1100 2ND", "1100 2ND", "1100 2ND",
"1100 2ND", "1100 2ND", "1100 2ND"), Item = structure(c(2L,
2L, 1L, 2L, 2L, 2L), .Label = c("Beverage", "Food"), class = "factor"),
Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "2019", class = "factor")), row.names = c(NA,
-6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000233561b1ef0>)
我想从DF2中提取pop col,并根据DF2中填充的订单中的“Building”和“Month”将其添加到数据帧1中
我尝试使用merge执行此命令,但执行时col为NULL:
df_2019_final1$Population <- df_2019_pop$Population[match(df_2019_final1$Month, df_2019_pop$Month, df_2019_final1$Building, df_2019_pop$Building)]
subset_df_pop <- df_2019_pop[, c("Month", "Building", "Population")]
updated_2019_test <- merge(df_2019_final1, subset_df_pop, by = c('Month', 'Building'))
df_2019_final1$Population在其中一个数据集中,“月”是缩写,第二个是全名。我们可以调整到这些格式中的任何一种,而合并
将起作用
df2$MonthN <- month.abb[match(df2$Month, month.name)]
library(dplyr)
left_join(df1, df2[, c("MonthN", "Building", "Population")],
by = c('Month' = 'MonthN', 'Building'))
注意:合并数据集上的人口列将是基于示例的NA
,因为子集数据集中的“建筑”值不同match
只接受2个向量match(x,table,nomatch=NA\u integer\uuu,comparables=NULL)
您可以尝试合并(df1,df2,by=c('Month',Building'))
我刚刚尝试合并,结果它输出了一个空白的df。由于某些原因,它将所有列都带过来了,而不是月份/建筑。您需要通过只包含这些列+人口来对第二个数据进行子测试,即,merge(df1,df2[,c(“月”,“建筑”,“人口”)],by=c(“月”,“建筑”)
我用你的解决方案编辑了我的问题,之后仍然得到了一个空白的df。非常感谢你的帮助-匹配的建筑会填充人口吗?@Dinho在这种情况下,您只需将
与合并“月”,即通过从中删除构建,不幸的是,我仍然无法获得所需的输出。我试图从主df获得相同的布局,但只是将pop数据添加到相应的区域。我想我需要移除不匹配的建筑才能做到这一点work@Dinho这是一个不同的问题,但它只能根据那些by
variables的匹配值进行匹配。我让它工作了-我对两个DFs的“构建”在字体方面是不同的(一个是全部大写,另一个不是)。使用Toupper和wa可以为匹配的建筑ID填充POP。谢谢你帮助我的人!
df2$MonthN <- month.abb[match(df2$Month, month.name)]
library(dplyr)
left_join(df1, df2[, c("MonthN", "Building", "Population")],
by = c('Month' = 'MonthN', 'Building'))
merge(df1, df2[, c("MonthN", "Building", "Population")],
by.x = c('Month', 'Building'), by.y = c('MonthN', 'Building'), all.x = TRUE)