Have to simplify a previous question that failed.
I want to extract whole groups, identified by 'id', that contain a string ('inter' or 'high') in another column called 'strmatch'. The string doesn't occurr in every observation of the group, but if it occurs I want to assign the group to a respective data frame.
The data frame
df <- data.frame(id = c("a", "a", "b", "b","c", "c","d","d"),
std = c("y", "y","n","n","y","y","n","n"),
strmatch = c("alpha","TMB-inter","beta","TMB-high","gamma","delta","epsilon","TMB-inter"))
Looks like this
id std strmatch
a y alpha
a y TMB-inter
b n beta
b n TMB-high
c y gamma
c y delta
d n epsilon
d n TMB-inter
Expected result
dfa
id std strmatch
a y alpha
a y TMB-inter
d n epsilon
d n TMB-inter
dfb
id std strmatch
b n beta
b n TMB-high
dfc
id std strmatch
c y gamma
c y delta
What I've tried
split(df, grepl("high", df$strmatch))
Gives only two data frames, one with a row containing 'high' and the other one with the rest.
Thanks a lot for your help.
You could maybe divide this into two parts. First find out values which match
"inter|high"
and break them into separate dataframes and then find the one which do not match any ofunique_vals
.