有沒有辦法left_join將任何 NA 值視為通配符(即全部匹配)。
例如,我有一個 tibble df_x:
df_x<-tribble(
~`Employee Type`, ~`Employee Name`, ~`Employee Department`,
"Manager" , "Bob" , "Accounting",
"Junior" , "Keith" , "Accounting",
"Manager" , "Alice" , "Finance" ,
"Junior" , "Robert" , "Finance" ,
"Manager" , "Claire" , "I.T." ,
"Senior" , "Ashley" , "I.T." ,
"Junior" , "Sam" , "I.T." ,
"Junior" , "Joe" , "I.T."
)
另一個tibble df_y,指定不同部門不同角色的工資,其中所有經理都應該得到60000,無論他們在哪個部門作業:
df_y<-tribble(
~`Employee Type`, ~`Employee Department`, ~Salary,
"Manager" , NA , 60000 ,
"Senior" , "I.T." , 40000 ,
"Junior" , "I.T." , 30000 ,
"Junior" , "Finance" , 35000 ,
"Junior" , "Accounting" , 35000
)
我想要的是,在 left_join 之后,我會得到:
> left_join(df_x,df_y, *magic argument here*)
Joining, by = c("Employee Type", "Employee Department")
# A tibble: 8 x 4
`Employee Type` `Employee Name` `Employee Department` Salary
<chr> <chr> <chr> <dbl>
1 Manager Bob Accounting 60000
2 Junior Keith Accounting 35000
3 Manager Alice Finance 60000
4 Junior Robert Finance 35000
5 Manager Claire I.T. 60000
6 Senior Ashley I.T. 40000
7 Junior Sam I.T. 30000
8 Junior Joe I.T. 30000
但我實際得到的是:
> left_join(df_x,df_y)
Joining, by = c("Employee Type", "Employee Department")
# A tibble: 8 x 4
`Employee Type` `Employee Name` `Employee Department` Salary
<chr> <chr> <chr> <dbl>
1 Manager Bob Accounting NA
2 Junior Keith Accounting 35000
3 Manager Alice Finance NA
4 Junior Robert Finance 35000
5 Manager Claire I.T. NA
6 Senior Ashley I.T. 40000
7 Junior Sam I.T. 30000
8 Junior Joe I.T. 30000
有沒有辦法指定“如果部門是 NA,那么無論如何都要應用查找”。或者我是否以錯誤的方式看待這個問題并為作業使用了錯誤的功能?有沒有更“tidyverse”的方法來解決這個問題?
uj5u.com熱心網友回復:
我只會加入型別,然后過濾:
df_x %>%
inner_join(df_y, by = "Employee Type", suffix = c("", "_y")) %>%
filter(is.na(`Employee Department_y`) | `Employee Department` == `Employee Department_y`) %>%
select(-`Employee Department_y`)
uj5u.com熱心網友回復:
一個可能的解決方案:
library(dplyr)
left_join(df_x,df_y) %>%
mutate(Salary = ifelse(is.na(Salary),60000,Salary))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/324564.html
下一篇:合并具有不同列的資料框
