How can I creat a list from another one, selecting specific conditions using R?

Viewed 25

I have a big list of data, in which 3 variables are present:

Conditions Rep RpL Answer
prot1, 1, 1, 53  
prot1, 2, 1, 53  
prot1, 1, 2, 54  
prot1, 2, 2, 36  
prot2, 1, 1, 55  
prot2, 2, 1, 50  
prot2 1 2 62 
...

For now, to choose which conditions I want to plot, i'm using the simple:

c<- b[b$Rep %in% c("1", "2", "3"),]

But I would like to separate the repetitions (Rep) depending also on the conditions on the same plot.

For example, I would like to select for the prot 1, the rep 1, 2 and 3 (as I've shown on the array), but for the prot2, I would like to select the rep 3 and 4, in the same list. Is it possible? Because on the way I am doing, I am selecting the rep 1, 2 and 3 for all the conditions, it's not what I want.

1 Answers

You can mention the conditons in subset :

subset(df, (Conditions == 'prot1' & Rep %in% 1:3) | 
           (Conditions == 'prot2' & Rep %in% 3:4))

Or in dplyr using filter

library(dplyr)
df %>%
  filter((Conditions == 'prot1' & Rep %in% 1:3) | 
         (Conditions == 'prot2' & Rep %in% 3:4))
Related