我尝试使用指定合并键的dplyr中的left_join
该函数工作正常,但我遇到的问题是我想在函数外部设置合并键(即作为变量client_1_key和client_2_key)。不幸的是,我在解决语法问题方面遇到了问题。任何帮助将不胜感激
client_1_key <- "customer_hashed"
client_2_key <- "customer_identifier"
client_merged <- left_join(client_1_file,
client_2_file,
by = c("customer_hashed" = "customer_identifier"))
示例client_1_file:
来源:本地数据框[24 x 1]
customer_hashed
1
2
3
4
5
示例客户端2文件:
来源:本地数据框[24 x 5]
customer_identifier health_insurance pet_insurance life_insurance car_insurance
1 1 Y N N Y
2 2 N N N Y
3 3 N N Y N
4 4 Y N N N
5 15 Y N Y N
答案 0 :(得分:2)
您可以尝试
left_join(client_1_file, client_2_file,
by= setNames(client_2_key, client_1_key))
# customer_hashed health_insurance pet_insurance life_insurance car_insurance
#1 1 Y N N Y
#2 2 N N N Y
#3 3 N N Y N
#4 4 Y N N N
#5 5 <NA> <NA> <NA> <NA>