Iterate over a list of dataframes and get a subset

2019-08-07 13:05发布

问题:

I have a list of dataframes (mylist) and i want to iterate over them and get all the rows from each dataframe that in a specific column don't have some values("upstream" or "downstream")!I would like to return the subsets (for the rest of the values) of dataframes back to the list! For the moment i am doing that:

mylist<- lapply(mylist, function(df){
  if (df$column != "upstream" | "downstream"){
     mylist <- df
  }
})

Which gives the following error:

Error in df$insideFeature : object of type 'symbol' is not subsettable

Thanks you in advance for any help provided!

回答1:

As PeterDee also noted: you are using | incorrectly. Use %in% to do this as follows

myfun <- function(df, words) {
  good_col <- which(! df$column %in% words)
  df[good_col,]
}

and then

lapply(mylist, myfun, c("upstream", "downstream"))