NEWSCIENT
NEWSCIENT

Reputation: 57

Iterate over a list of dataframes and get a subset of each dataframe in R

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!

Upvotes: 1

Views: 780

Answers (1)

Rentrop
Rentrop

Reputation: 21497

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"))

Upvotes: 1

Related Questions