简体   繁体   中英

How to grep a group based on string in another column that doesn't occur in each observation using R?

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 of unique_vals .

unique_vals <- unique(grep("inter|high", df$strmatch, value = TRUE))

c(lapply(unique_vals, function(x) subset(df, id %in% id[strmatch == x])), 
         list(subset(df, !id %in% id[strmatch %in% unique_vals])))


#[[1]]
#  id std  strmatch
#1  a   y     alpha
#2  a   y TMB-inter
#7  d   n   epsilon
#8  d   n TMB-inter

#[[2]]
#  id std strmatch
#3  b   n     beta
#4  b   n TMB-high

#[[3]]
#  id std strmatch
#5  c   y    gamma
#6  c   y    delta

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM