0

I would like to create a new dataframe from existing one. My df looks like this:

X                Y
12            ABC_SS
23            B49
45            G56_SS

I would like to subset new dataset that will have only Y values that includes "_SS" in Y. How can i do that, please?

This doesnt work:

newdf <-subset(df,df$Y %in% "_SS")
rama27
  • 115
  • 1
  • 1
  • 6

1 Answers1

3

We can use grepl in base R to match substring

subset(df, grepl("_SS", Y))

Or another option is filter

library(dplyr)
library(stringr)
df %>%
     filter(str_detect(Y, "_SS$"))
akrun
  • 874,273
  • 37
  • 540
  • 662