Home > Net >  filter by how many signs are in a row in r
filter by how many signs are in a row in r

Time:11-28

Is it possible to filter by how many signs are in a row in r and also if a certain letter (or word) is in the row?

Like for example if I only wanted to filter rows with 2 or more signs and they also have to include c.

Example input:

a   b   c 
a   b 
a   c 
a   b   b 
a   c   c 
a   b   c   d 

Example output:

a   b   c 
a   c   c 
a   b   c   d 

CodePudding user response:

We use str_count to get the number of in each element of the column, check whether it is greater than 1, as well as detect for 'c' character in the column

library(dplyr)
library(stringr)
df1 %>%
    filter(str_count(col1, fixed(" ")) >1, str_detect(col1, fixed("c")))

-output

# A tibble: 3 × 1
  col1         
  <chr>        
1 a   b   c    
2 a   c   c    
3 a   b   c   d

data

df1 <- structure(list(col1 = c("a   b   c", "a   b", "a   c", "a   b   b", 
"a   c   c", "a   b   c   d")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L))

CodePudding user response:

Here is a possible base R solution:

df[nchar(gsub("[^ ]", "", df$col1)) > 1 &
      grepl("c", df$col1, fixed = TRUE), ]

Output

  col1         
  <chr>        
1 a   b   c    
2 a   c   c    
3 a   b   c   d

Data

structure(list(col1 = c("a   b   c", "a   b", "a   c", "a   b   b", 
"a   c   c", "a   b   c   d")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L))
  • Related