替换与R中另一个data.frame的多个列匹配的列中的值 [英] Replacing values in a column that match multiple columns of another data.frame in R

查看:132
本文介绍了替换与R中另一个data.frame的多个列匹配的列中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 dplyr 包中的合并选项和联合选项,但是我找不到想要的合并类型。

I have been trying with merge and joint options from dplyr package, but I can't get the type of merging I'm looking for.

例如,考虑 data.frame

df1 <- data.frame(Country=c(rep("AFG",3),rep("AUS",3)), Category=(rep(c("a","b","c"),2)), value=c(1:6), othr=c(10:15)) 

和另一个 data.frame

df2 <- data.frame(Country=c(rep("AFG",2)), Category=c("a","b"), value=c(7,8))

理想的输出如下:

   Country Category value othr
1     AFG        a     7   10
2     AFG        b     8   11
3     AFG        c     3   12
4     AUS        a     4   13
5     AUS        b     5   14
6     AUS        c     6   15

即,<$ c $ AFG-a和AFG-b中的c> df1 $ value 替换为 df2 $ val ue 。我想将此操作包含在一个循环中,以基于国家和类别列进行很多更改(在我的实际数据中,有两个以上的列需要匹配)

i.e., df1$value is replaced in AFG-a and AFG-b by df2$value. I want to include this operation in a loop making many changes based on "Country" and "Category" columns (in my real data there are more than two columns to be matched)

非常感谢!

推荐答案

有了 dplyr ,我们可以使用 coalesce

library(dplyr)

df1 %>% 
  full_join(df2, by = c('Country', 'Category'), suffix = c('', '.1')) %>%
  mutate_if(is.numeric, as.numeric) %>%
  mutate(value = coalesce(value.1, value)) %>% 
  select(-value.1)

哪个返回:

  Country Category value othr
1     AFG        a     7   10
2     AFG        b     8   11
3     AFG        c     3   12
4     AUS        a     4   13
5     AUS        b     5   14
6     AUS        c     6   15

带有 data.table ,我们可以这样做:

With data.table, we could do:

library(data.table)

dt1 <- setDT(df1)
dt2 <- setDT(df2)

dt1[dt2, on = c("Country", "Category"), value := i.value]

返回值:

   Country Category value othr
1:     AFG        a     7   10
2:     AFG        b     8   11
3:     AFG        c     3   12
4:     AUS        a     4   13
5:     AUS        b     5   14
6:     AUS        c     6   15

数据:

df1 <- data.frame(
  Country = c(rep("AFG", 3), rep("AUS", 3)),
  Category = (rep(c("a", "b", "c"), 2)),
  value = c(1:6),
  othr = c(10:15),
  stringsAsFactors = FALSE
)

df2 <- data.frame(
  Country = c(rep("AFG", 2)),
  Category = c("a", "b"),
  value = c(7, 8),
  stringsAsFactors = FALSE
)

这篇关于替换与R中另一个data.frame的多个列匹配的列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆