Using regexp to select rows in R dataframe

前端 未结 7 1105
终归单人心
终归单人心 2020-12-08 00:48

I\'m trying to select rows in a dataframe where the string contained in a column matches either a regular expression or a substring:

dataframe:

7条回答
  •  太阳男子
    2020-12-08 01:51

    This is a pretty minimal solution using dplyr and magrittr which I think is what you are after:

    Data:
    library(magrittr)
    library(stringr)
    dat <- read.table(text="
    aName   bName   pName   call  alleles   logRatio    strength
                      AX-11086564 F08_ADN103  2011-02-10_R10  AB  CG  0.363371    10.184215
                      AX-11086564 A01_CD1919  2011-02-24_R11  BB  GG  -1.352707   9.54909
                      AX-11086564 B05_CD2920  2011-01-27_R6   AB  CG  -0.183802   9.766334
                      AX-11086564 D04_CD5950  2011-02-09_R9   AB  CG  0.162586    10.165051
                      AX-11086564 D07_CD6025  2011-02-10_R10  AB  CG  -0.397097   9.940238
                      AX-11086564 B05_CD3630  2011-02-02_R7   AA  CC  2.349906    9.153076
                      AX-11086564 D04_ADN103  2011-02-10_R2   BB  GG  -1.898088   9.872966
                      AX-11086564 A01_CD2588  2011-01-27_R5   BB  GG  -1.208094   9.239801
                      ", header=TRUE)
    

    rows that contain ADN in column bName.

    dat %>%
      filter(str_detect(bName, "ADN") == TRUE)
    

    Secondarily, I would like all rows that contain ADN in column bName and that match 2011-02-10_R2 in column pName.

    dat %>%
      filter(str_detect(bName, "ADN") & pName == "2011-02-10_R2") 
    

提交回复
热议问题