【问题标题】:How to match two string of a column and filter the matched columns values如何匹配列的两个字符串并过滤匹配的列值
【发布时间】:2020-07-10 00:49:53
【问题描述】:

我有一个包含 1000 个员工详细信息的数据框 其中有一列 Tenure 和 Month_count 并且想要匹配 Month1 = 1、Month2 = 2、Month3 = 3 和 Experienced = 4 的条目。如果匹配,我只需要过滤这些行。

name<-c(rep("Bob", 4),rep("Dick", 6),rep("Jack",5),rep("ryan",4))
name<-as.data.frame(name)
Tenure<-c("Month1","Month2","Month3","Experienced","Month2","Month3","Experienced",
          "Experienced","Experienced","Experienced","Month1","Month2","Month3","Experienced","Experienced","Experienced","Experienced","Experienced","Experienced")
Tenure<-as.data.frame(Tenure)
Month_count<-c(seq(1:4),seq(2,7,by=1),seq(1:5),seq(1:4))
Month_count<-as.data.frame(Month_count)
total<-cbind(name,Tenure,Month_count)


下面的输入和所需的输出

如果有任何 dplyr 解决方案,我将不胜感激

【问题讨论】:

    标签: r dplyr tidyverse


    【解决方案1】:

    您可以在filter中添加条件:

    library(dplyr)
    total %>%
      filter(Tenure == 'Month1' & Month_count == 1 | 
             Tenure == 'Month2' & Month_count == 2 |
             Tenure == 'Month3' & Month_count == 3 |
             Tenure == 'Experienced' & Month_count == 4)
    
    #   name      Tenure Month_count
    #1   Bob      Month1           1
    #2   Bob      Month2           2
    #3   Bob      Month3           3
    #4   Bob Experienced           4
    #5  Dick      Month2           2
    #6  Dick      Month3           3
    #7  Dick Experienced           4
    #8  Jack      Month1           1
    #9  Jack      Month2           2
    #10 Jack      Month3           3
    #11 Jack Experienced           4
    #12 ryan Experienced           4
    

    或在subset 中使用相同的内容以将其保留在基础 R 中:

    subset(total, Tenure == 'Month1' & Month_count == 1 | 
                  Tenure == 'Month2' & Month_count == 2 |
                  Tenure == 'Month3' & Month_count == 3 |
                  Tenure == 'Experienced' & Month_count == 4)
    

    【讨论】:

      【解决方案2】:

      我们可以使用 Map 自动执行此操作

      v1 <- c(paste0("Month", 1:3), "Experienced")
      v2 <- 1:4
      total[Reduce(`|`, Map(function(x, y) with(total,
                   Tenure == x & Month_count ==y), v1, v2)),]
      #   name      Tenure Month_count
      #1   Bob      Month1           1
      #2   Bob      Month2           2
      #3   Bob      Month3           3
      #4   Bob Experienced           4
      #5  Dick      Month2           2
      #6  Dick      Month3           3
      #7  Dick Experienced           4
      #11 Jack      Month1           1
      #12 Jack      Month2           2
      #13 Jack      Month3           3
      #14 Jack Experienced           4
      #19 ryan Experienced           4
      

      或使用tidyverse

      library(dplyr)
      library(purrr)
      total %>% 
            filter(map2(v1, v2, ~ Tenure == .x & Month_count == .y) %>%
                     reduce(`|`))
      #    name      Tenure Month_count
      #1   Bob      Month1           1
      #2   Bob      Month2           2
      #3   Bob      Month3           3
      #4   Bob Experienced           4
      #5  Dick      Month2           2
      #6  Dick      Month3           3
      #7  Dick Experienced           4
      #8  Jack      Month1           1
      #9  Jack      Month2           2
      #10 Jack      Month3           3
      #11 Jack Experienced           4
      #12 ryan Experienced           4
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-29
        • 2017-12-05
        • 2021-09-10
        相关资源
        最近更新 更多