Home > Software engineering >  Subset a row with two observations together
Subset a row with two observations together

Time:01-16

I have a dataset where a column contains rows of observations. Some of the rows have single values while others have two values. I want to subset only those rows that contain two observations together. Eg.

test <- data.frame(value=c("A1", "B1", "C1", "A1,C1", "C1", "C1, B1", "D1", "F1,E1"))

The answer I want is "A1,C1", "C1, B1", "F1,E1".

CodePudding user response:

If the multiple values are always separated by a comma, we can filter for rows that contain comma.

library(dplyr)

test %>% filter(grepl(",", value))

   value
1  A1,C1
2 C1, B1
3  F1,E1

CodePudding user response:

I though about two different approaches:

test <- data.frame(value=c("A1", "B1", "C1", "A1,C1", "C1", "C1, B1", "D1", "F1,E1"))

library(dplyr)

#By number of characters
test %>% 
  filter(nchar(value) > 2)


#By detecting the comma
test %>% 
  filter(stringr::str_detect(value,","))
  • Related