0

I have a list of objects, where each object is a dataframe.

list_of_dfs<-(df1, df2, df3,....)

Suppose I want to extract information from the first column of each dataframe, say suppose extract all values of column 1 which is above 0.5, how do I use a for loop to do that?

Complication: It might be that each dataframe is only a "name" in the list_of_dfs however running the name itself in the console e.g. '>df1' would generate dataframe information.

Thanks for your consideration and advice.

Peter
  • 11,500
  • 5
  • 21
  • 31
  • 1
    Welcome to SO! In general you can do this with `lapply`, or loops, but it would help if you provided a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your data. It's hard to produce code without knowing things like, do the data frames have the same shapes or column names, can there be data frames with no rows, or missing columns? Also your complication changes the question quite a lot - it can still be done but it's two questions really. – SamR May 11 '22 at 10:25

1 Answers1

1

If you first columns have all the same name, e.g. col_1 you could use a map to apply filter to all of them at once

library(tidyverse)
list_of_dfs <- (df1, df2, df3,....)
map(list_of_dfs, filter, col_1 > 0.5)
map(list_of_dfs, ~ filter(., col_1 > 0.5)) # equivalent
abichat
  • 2,317
  • 2
  • 21
  • 39