Selecting data frame rows based on partial string match in a column

后端 未结 4 1260
轻奢々
轻奢々 2020-11-22 09:16

I want to select rows from a data frame based on partial match of a string in a column, e.g. column \'x\' contains the string "hsa". Using sqldf -

4条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 09:55

    Try str_detect() from the stringr package, which detects the presence or absence of a pattern in a string.

    Here is an approach that also incorporates the %>% pipe and filter() from the dplyr package:

    library(stringr)
    library(dplyr)
    
    CO2 %>%
      filter(str_detect(Treatment, "non"))
    
       Plant        Type  Treatment conc uptake
    1    Qn1      Quebec nonchilled   95   16.0
    2    Qn1      Quebec nonchilled  175   30.4
    3    Qn1      Quebec nonchilled  250   34.8
    4    Qn1      Quebec nonchilled  350   37.2
    5    Qn1      Quebec nonchilled  500   35.3
    ...
    

    This filters the sample CO2 data set (that comes with R) for rows where the Treatment variable contains the substring "non". You can adjust whether str_detect finds fixed matches or uses a regex - see the documentation for the stringr package.

提交回复
热议问题