覆盖left_join dplyr以更新数据

马丁

我的问题与此类似,但是我在LHS中还有其他应保留的列https://stackoverflow.com/a/35642948/9285732

yval1的x更新值的的子集在此,我想覆盖相关值,但保留其余值。x

样本数据:

library(tidyverse)

x <- tibble(name = c("hans", "dieter", "bohlen", "hans", "dieter", "alf"), 
            location = c(1,1,1,2,2,3), 
            val1 = 1:6, val2 = 1:6, val3 = 1:6)
y <- tibble(name = c("hans", "dieter", "hans"), 
            location = c(2,2,1), 
            val1 = 10)
> x
# A tibble: 6 x 5
  name   location  val1  val2  val3
  <chr>     <dbl> <int> <int> <int>
1 hans          1     1     1     1
2 dieter        1     2     2     2
3 bohlen        1     3     3     3
4 hans          2     4     4     4
5 dieter        2     5     5     5
6 alf           3     6     6     6

> y
# A tibble: 3 x 3
  name   location  val1
  <chr>     <dbl> <dbl>
1 hans          2    10
2 dieter        2    10
3 hans          1    10

> # desired output
> out
# A tibble: 6 x 5
  name   location  val1  val2  val3
  <chr>     <dbl> <dbl> <int> <int>
1 hans          1    10     1     1
2 dieter        1     2     2     2
3 bohlen        1     3     3     3
4 hans          2    10     4     4
5 dieter        2    10     5     5
6 alf           3     6     6     6

我编写了一个可以执行我想要的功能的函数,但是它很麻烦。我想知道是否有我不知道的更优雅的方法甚至是dplyr函数。

overwrite_join <- function(x, y, by = NULL){

  bycols     <- which(colnames(x) %in% by) 
  commoncols <- which(colnames(x) %in% colnames(y))
  extracols  <- which(!(colnames(x) %in% colnames(y)))

  x1 <- anti_join(x, y, by = by) %>% 
    bind_rows(y) %>%
    select(commoncols) %>% 
    left_join(x %>% select(bycols, extracols), by = by)

  out <- x %>% select(by) %>% 
    left_join(x1, by = by)

  return(out)
}

overwrite_join(t1, t2, by = c("name", "location"))
它的

您可以按照以下方式进行操作

> x %>%
    left_join(y = y, by = c("name", "location")) %>%
    within(., val1.x <- ifelse(!is.na(val1.y), val1.y, val1.x)) %>%
    select(-val1.y)
# # A tibble: 6 x 5
#   name   location val1.x  val2  val3
#   <chr>     <dbl>  <dbl> <int> <int>
# 1 hans          1     10     1     1
# 2 dieter        1      2     2     2
# 3 bohlen        1      3     3     3
# 4 hans          2     10     4     4
# 5 dieter        2     10     5     5
# 6 alf           3      6     6     6

然后重命名val1.x。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

用dplyr的left_join编写函数

dplyr left_join小于,大于条件

dplyr left_join()按行名

dplyr left_join匹配不适用

left_join (dplyr) 使用函数

合并和dplyr的left_join的异常行为

dplyr::left_join() 产生意外错误

dplyr的left_join行为不正确?

在自定义函数中使用 dplyr::left_join

复杂的绑定列(使用 dplyr 使用 left_join 和 bind cols 重新组织数据)

匹配两个数据表(Vlookup、dplyr、match()、left_join)保持行数

使用left_join()连接两个数据帧

left_join不同键上的数据帧的多重时间

使用 left_join 提取数据时的困难

在dplyr的left_join中使用as_label / as_name而不是quo_name连接两个数据集

R - dplyr left_join() - 多重匹配 - 如何重新组合...?

使用 by=c(x=y) 错误在函数内执行 dplyr::left_join

dplyr left_join 函数不完全加入,看似随机的 NA

将dplyr中的left_join与指定的合并变量一起使用

使用 data.table x[y] 语法复制 dplyr::left_join() 列顺序

left_join(dplyr)下一个可用日期

r dplyr::left_join 使用日期时间列不能正确连接

dplyr`left_join()`不适用于字符对象作为LHS变量

如何根据行名合并/left_join多个数据框

Left_join与Df1和相等数据帧的列表

R:结合lapply和left_join来有条件地合并数据帧

使用POSIXct格式化两个不同的数据帧以进行left_join()-ing

如何使用 dplyr left_join 在数据框中添加属性作为列表元素并根据需要将其导出到文本文件中?

Left_join用第二个数据帧中的数据值填充NA条目