合并到一组数据并替换当前值,如果在更新列中

发布于 2025-02-13 23:21:15 字数 379 浏览 2 评论 0原文

使用以下数据帧。我想创建一个新数据

df1 <- data.frame(ID = LETTERS[1:10],Quantity = c(1,2,3,4,5,6,7,8,9,10))
df2 <- data.frame(Cur_ID = c("A","C","D","H"), Update_ID = c("A","C","X","Y"), Update_Quantity = c(20, 21, 22, 23))

desired df3:
ID  letters
A   20
B   2
C   21
X   22
E   5
F   6
G   7
Y   23
I   9
J   10

框DF2中的所有内容均为更新,如果update_id与cur_id不同。

With the following data frames. I would like to create a new data frame where quantities for IDs in df2 are updated with the 'Update quantities' values and IDs are replaces with the 'Update_ID' values (which may or may not change from the current ID.

df1 <- data.frame(ID = LETTERS[1:10],Quantity = c(1,2,3,4,5,6,7,8,9,10))
df2 <- data.frame(Cur_ID = c("A","C","D","H"), Update_ID = c("A","C","X","Y"), Update_Quantity = c(20, 21, 22, 23))

desired df3:
ID  letters
A   20
B   2
C   21
X   22
E   5
F   6
G   7
Y   23
I   9
J   10

i.e. the quantities for all in df2 are updates, and the IDs if Update_ID is different from Cur_ID.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒲公英的约定 2025-02-20 23:21:15

使用dplyr,您可以合并df2df1,并使用coalesce()更新id id 和数量

library(dplyr)

df1 %>%
  left_join(df2, by = c("ID" = "Cur_ID")) %>%
  mutate(ID = coalesce(Update_ID, ID),
         Quantity = coalesce(Update_Quantity, Quantity), .keep = "unused")

带有PowerJoin的另一个选项:

library(powerjoin)

power_left_join(df1, setNames(df2, c("Cur_ID", "ID", "Quantity")),
                by = c("ID" = "Cur_ID"), conflict = coalesce_yx)
输出
#    ID Quantity
# 1   A       20
# 2   B        2
# 3   C       21
# 4   X       22
# 5   E        5
# 6   F        6
# 7   G        7
# 8   Y       23
# 9   I        9
# 10  J       10

With dplyr, you could merge df2 into df1 and use coalesce() to update ID and Quantity.

library(dplyr)

df1 %>%
  left_join(df2, by = c("ID" = "Cur_ID")) %>%
  mutate(ID = coalesce(Update_ID, ID),
         Quantity = coalesce(Update_Quantity, Quantity), .keep = "unused")

Another option with powerjoin:

library(powerjoin)

power_left_join(df1, setNames(df2, c("Cur_ID", "ID", "Quantity")),
                by = c("ID" = "Cur_ID"), conflict = coalesce_yx)
Output
#    ID Quantity
# 1   A       20
# 2   B        2
# 3   C       21
# 4   X       22
# 5   E        5
# 6   F        6
# 7   G        7
# 8   Y       23
# 9   I        9
# 10  J       10
︶ ̄淡然 2025-02-20 23:21:15

使用data.table JOIN

library(data.table)
setDT(df1)[df2, c('ID', 'Quantity') := .(Update_ID, 
         Update_Quantity), on = .(ID = Cur_ID)]

-OUTPUT

> df1
        ID Quantity
    <char>    <num>
 1:      A       20
 2:      B        2
 3:      C       21
 4:      X       22
 5:      E        5
 6:      F        6
 7:      G        7
 8:      Y       23
 9:      I        9
10:      J       10

Using data.table join

library(data.table)
setDT(df1)[df2, c('ID', 'Quantity') := .(Update_ID, 
         Update_Quantity), on = .(ID = Cur_ID)]

-output

> df1
        ID Quantity
    <char>    <num>
 1:      A       20
 2:      B        2
 3:      C       21
 4:      X       22
 5:      E        5
 6:      F        6
 7:      G        7
 8:      Y       23
 9:      I        9
10:      J       10
提笔落墨 2025-02-20 23:21:15

采用基本方法:

type.convert(         
  data.frame(
    t(with(df2, apply(df1, 1, \(x) {
      if (x[1] %in% Cur_ID)
      {
        y <- x[1];
        x[1] <- Update_ID[Cur_ID == x[1]]; 
        x[2] <- Update_Quantity[Cur_ID == y]
      }
      x
    })))), 
  as.is = T
)

#>    ID Quantity
#> 1   A       20
#> 2   B        2
#> 3   C       21
#> 4   X       22
#> 5   E        5
#> 6   F        6
#> 7   G        7
#> 8   Y       23
#> 9   I        9
#> 10  J       10

With a base R approach:

type.convert(         
  data.frame(
    t(with(df2, apply(df1, 1, \(x) {
      if (x[1] %in% Cur_ID)
      {
        y <- x[1];
        x[1] <- Update_ID[Cur_ID == x[1]]; 
        x[2] <- Update_Quantity[Cur_ID == y]
      }
      x
    })))), 
  as.is = T
)

#>    ID Quantity
#> 1   A       20
#> 2   B        2
#> 3   C       21
#> 4   X       22
#> 5   E        5
#> 6   F        6
#> 7   G        7
#> 8   Y       23
#> 9   I        9
#> 10  J       10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文