Home > Software engineering >  R: How can I filter out a specific variable value from each table in my list of tables?
R: How can I filter out a specific variable value from each table in my list of tables?

Time:03-04

Suppose you had a list of 200 tables each containing the variable "cyl". In the variable "cyl", there are three values ("4", "6", and "8"). In R, how could I filter out the value "4" from each of the 200 tables?

The code for the list generation is provided below.

output <- list()

iterations <- 200

for(i in 1:iterations){
  output[[i]] <-  mtcars <- mtcars[sample(nrow(mtcars), size = 15, replace = FALSE), ]

}

Any help at all is greatly appreciated!

CodePudding user response:

Turning my comment into an answer, if you have an operation you'd like to apply over a list of items, lapply is usually the right choice:

lapply(output, subset, cyl != 4)
  •  Tags:  
  • r
  • Related