【问题标题】:Using left_join from dplyr with merge variables specified使用 dplyr 中的 left_join 并指定合并变量
【发布时间】:2015-02-28 12:33:50
【问题描述】:

我正在尝试使用 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

【问题讨论】:

标签: r left-join dplyr


【解决方案1】:

你可以试试

 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2017-03-27
    相关资源
    最近更新 更多