How to split a dataframe based on a word (of a string) from a column in r

suppressPackageStartupMessages({
  library(dplyr)
  library(stringr)
})
# to import data to workspace only
df_ <- readr::read_csv("/home/roc/Desktop/grist.csv")
#> 
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#>   id = col_character(),
#>   name = col_character(),
#>   description = col_character(),
#>   parent_id = col_character()
#> )
df_
#> # A tibble: 5 x 4
#>   id    name  description                               parent_id
#>   <chr> <chr> <chr>                                     <chr>    
#> 1 001   A     A may increase the activities of B        009      
#> 2 002   E     A may be increased by the activities of C 013      
#> 3 007   F     A may decrease the activities of D        055      
#> 4 010   G     A may be decreased by the activities of G 067      
#> 5 011   K     A may increase the activities of X        100
df_ %>% filter(str_detect(description,"inc")) -> df1
df1
#> # A tibble: 3 x 4
#>   id    name  description                               parent_id
#>   <chr> <chr> <chr>                                     <chr>    
#> 1 001   A     A may increase the activities of B        009      
#> 2 002   E     A may be increased by the activities of C 013      
#> 3 011   K     A may increase the activities of X        100
df_ %>% filter(str_detect(description,"dec")) -> df2
df2
#> # A tibble: 2 x 4
#>   id    name  description                               parent_id
#>   <chr> <chr> <chr>                                     <chr>    
#> 1 007   F     A may decrease the activities of D        055      
#> 2 010   G     A may be decreased by the activities of G 067

Created on 2020-10-27 by the reprex package (v0.3.0.9001)

1 Like