【问题标题】:Mapping multiple columns in R including a single column containing comma-separated values映射 R 中的多列,包括包含逗号分隔值的单列
【发布时间】:2019-11-26 21:14:10
【问题描述】:

此问题涉及在遵守某些约束的同时映射多个列。这对我来说看起来很困难,因为我不确定如何在cat 中取消嵌套region 列并允许匹配df 数据。

问题:我想将数据框df 中的三列与数据框cat 中的四列相匹配。

结果应符合以下条件:

  1. loss_date in df 应该 >= StartDate 和 EndDate in cat
  2. df 中的 line_of_business 应该与 cat 中的 line_of_business 匹配。
  3. region in df 应该匹配 region in cat(注意:region in cat 需要取消嵌套)。

如果满足所有三个条件,则输出列应返回逻辑TRUE,否则应返回逻辑FALSE

数据:

df
loss_date   line_of_business            region
10/8/2018   Property                      NB
1/31/2019   Commercial Property Line      BC
8/1/2018    Auto                          AB
3/20/2019   Personal Property             ON
11/10/2018  Homeowners Line             Alberta

cat
Start Date  End Date    line_of_business            region
11/1/2018   11/30/2018  Homeowners Line            Alberta
10/1/2018   10/21/2018  Property                 AB,BC,MB,ATL,ON,PQ
4/9/2018    4/10/2018   Commercial Property         ATL
4/26/2018   5/22/2018   Auto                   BC, AB, PQ, ON, ATL

输出:

loss_date   line_of_business            region      output
10/8/2018   Property                    MB          TRUE
1/31/2019   Commercial Property Line    BC          FALSE
8/1/2018    Auto                        AB          FALSE
3/20/2019   Personal Property           ON          FALSE
11/10/2018  Homeowners Line          Alberta         TRUE

非常感谢您的帮助。

【问题讨论】:

    标签: r


    【解决方案1】:

    这是使用来自tidyrdplyrseparate_rows 的一种方法。我们将来自region 列的逗号分隔值带入不同的行,并与df 进行连接。使用as.Date 转换日期并检查loss_date 是否在StartDateEndDate 之间以及region 是否匹配。

    library(dplyr)
    
    cat %>%
      tidyr::separate_rows(region, sep = ",") %>%
      right_join(df, by = "line_of_business") %>%
      mutate_at(vars(ends_with("ate")), as.Date, "%m/%d/%Y") %>%
      group_by(line_of_business) %>%
      summarise(temp = any(region.x %in% region.y & loss_date >= StartDate & 
                           loss_date <= EndDate)) %>%
      left_join(df)
    
    #  line_of_business         temp  loss_date  region 
    #  <chr>                    <lgl> <chr>      <chr>  
    #1 Auto                     FALSE 8/1/2018   AB     
    #2 Commercial_Property_Line FALSE 1/31/2019  BC     
    #3 Homeowners_Line          TRUE  11/10/2018 Alberta
    #4 Personal_Property        FALSE 3/20/2019  ON     
    #5 Property                 TRUE  10/8/2018  MB    
    

    数据

    df <- structure(list(loss_date = c("10/8/2018", "1/31/2019", "8/1/2018", 
    "3/20/2019", "11/10/2018"), line_of_business = c("Property", 
    "Commercial_Property_Line", "Auto", "Personal_Property", "Homeowners_Line"
    ), region = c("MB", "BC", "AB", "ON", "Alberta")), row.names = c(NA, 
    -5L), class = "data.frame") 
    
    cat <- structure(list(StartDate = c("11/1/2018", "10/1/2018", "4/9/2018", 
    "4/26/2018"), EndDate = c("11/30/2018", "10/21/2018", "4/10/2018", 
    "5/22/2018"), line_of_business = c("Homeowners_Line", "Property", 
    "Commercial_Property", "Auto"), region = c("Alberta", "AB,BC,MB,ATL,ON,PQ", 
    "ATL", "BC,AB,PQ,ON,ATL")), row.names = c(NA, -4L), class = "data.frame")
    

    【讨论】:

      【解决方案2】:

      这是base R的解决方案:

      df$output <- apply(df, 1, function(v) 
              with(cat, any(difftime(as.Date(v[1],format="%m/%d/%Y"),as.Date(StartDate,format="%m/%d/%Y"))>=0 & 
                          difftime(as.Date(v[1],format="%m/%d/%Y"),as.Date(EndDate,format="%m/%d/%Y"))<=0 &
                          v[2] == line_of_business &
                          grepl(v[3],region))))
      

      屈服:

      > df
         loss_date         line_of_business  region output
      1  10/8/2018                 Property      MB   TRUE
      2  1/31/2019 Commercial_Property_Line      BC  FALSE
      3   8/1/2018                     Auto      AB  FALSE
      4  3/20/2019        Personal_Property      ON  FALSE
      5 11/10/2018          Homeowners_Line Alberta   TRUE
      

      【讨论】:

        猜你喜欢
        • 2011-08-02
        • 2020-08-01
        • 2022-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多