Search code examples
rdataframesubset

How can I subset a dataframe based on ID letters?


I have this df

df = data.frame(x = c('1E','2E','1F','2F'), y =1:4 )

and I wish to subset it into 2 dataframes based on the letters E and F

the resulted dataframes are as follows

   x y
1 1E 1
2 2E 2
   x y
1 1F 3
2 2F 4

Solution

  • You can use split with gsub. In the second argument, you can use whatever function that will make it so that you extract the letters of df$x:

    split(df, gsub("\\d", "", df$x))
    

    Use list2env if you want to subsequently convert the list into separate dataframes in your environment.

    list2env(split(df, gsub("\\d", "", df$x)), .GlobalEnv)