This is my first time asking a question here so please let me know if I need to change the way I am doing this. I have been looking for awhile and I haven't been able to find what I need.
I have a list of 3 dataframes. They have the same structure (variables) but not the same number of observations. I would like to get several subsets for each dataframe in my list, according to several conditions stored in a vector.
So if I have 5 conditions, I need to get, for each of the 3 dataframes in my list, 5 subsets of these dataframes, so 15 total.
For instance:
df1 <-data.frame(replicate(3,sample(0:10,10,rep=TRUE)))
df2 <-data.frame(replicate(3,sample(0:10,7,rep=TRUE)))
df3 <-data.frame(replicate(3,sample(0:10,8,rep=TRUE)))
my_list <- list(df1, df2, df3)
conditions <- c(2, 5, 7, 4, 6)
I know how to subset for one of the conditions using lapply
list_subset <- lapply(my_list, function(x) x[which(x$X1 == conditions[1]), ])
But I would like to do that for all the values in the vector conditions. I hope it makes sense.