gpt4 book ai didi

r - 覆盖 left_join dplyr 来更新数据

转载 作者:行者123 更新时间:2023-12-04 10:28:11 30 4
gpt4 key购买 nike

我的问题与此类似,但是我在 LHS 中还有其他列应该保留 https://stackoverflow.com/a/35642948/9285732
yx 的子集带有 val1 的更新值。在 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。

关于r - 覆盖 left_join dplyr 来更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59137949/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com