简体   繁体   English

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

[英]Conditionally replace cells in data frame based on another data frame

In the interest of learning better coding practices, can anyone show me a more efficient way of solving my problem?为了学习更好的编码实践,谁能告诉我一个更有效的方法来解决我的问题? Maybe one that doesn't require new columns...也许不需要新列的...

Problem: I have two data frames: one is my main data table (t) and the other contains changes I need to replace in the main table (Manual_changes).问题:我有两个数据框:一个是我的主数据表(t),另一个包含我需要在主表中替换的更改(Manual_changes)。 Example: Sometimes the CaseID is matched with the wrong EmployeeID in the file.示例:有时 CaseID 与文件中的错误 EmployeeID 匹配。

I can't provide the main data table, but the Manual_changes file looks like this:我无法提供主数据表,但 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

We could use coalesce我们可以使用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`))

Or with data.table或与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`)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM