gpt4 book ai didi

r - 根据另一个数据框有条件地替换数据框中的单元格

转载 作者:行者123 更新时间:2023-12-04 09:05:36 24 4
gpt4 key购买 nike

为了学习更好的编码实践,任何人都可以向我展示解决问题的更有效方法吗?也许一个不需要新列的...
问题:我有两个数据框:一个是我的主数据表 (t),另一个包含我需要在主表中替换的更改 (Manual_changes)。示例:有时 CaseID 与文件中错误的 EmployeeID 匹配。
我无法提供主数据表,但 Manual_changes 文件如下所示:

Manual_changes = structure(list(`Case ID` = c(46605, 25321, 61790, 43047, 12157, 
16173, 94764, 38700, 41798, 56198, 79467, 61907, 89057, 34232,
100189), `Employee ID` = c(NA, NA, NA, NA, NA, NA, NA, NA, 906572,
164978, 145724, 874472, 654830, 846333, 256403), `Age in Days` = c(3,
3, 3, 12, 0, 0, 5, 0, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA,
-15L), class = c("tbl_df", "tbl", "data.frame"))

temp = merge(t, Manual_changes, by = "Case ID", all.x = TRUE)
temp$`Employee ID.y` = ifelse(is.na(temp$`Employee ID.y`), temp$`Employee ID.x`, temp$`Employee ID.y`)
temp$`Age in Days.y`= ifelse(is.na(temp$`Age in Days.y`), temp$`Age in Days.x`, temp$`Age in Days.y`)
temp$`Age in Days.x` = NULL
temp$`Employee ID.x` = NULL
colnames(temp) = colnames(t)
t = temp

最佳答案

我们可以使用 coalesce

library(dplyr)
left_join(t, Manual_changes, by = "Case ID") %>%
mutate(Employee_ID.y = coalesce(`Employee ID.x`, `Employee ID.y`),
`Age in Days.y` = coalesce(`Age in Days.x`, `Age in Days.y`))

或与 data.table
library(data.table)
setDT(t)[Manual_changes,
c('Employee ID', 'Age in Days') :=
.(fcoalesce(`Employee ID.x`, `Employee ID.y`),
fcoalesce(`Age in Days.x`, `Age in Days.y`)),

on = .(`Case ID`)]

关于r - 根据另一个数据框有条件地替换数据框中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63457860/

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