0

I have a collection of ten dataframes (df.a, df.b, and so on) and the following concept in R

df.x.new = df.x %>% do this %>% do that...

I wondered if there is an elegant way to interchange the df.x variable of my single code line above iteratively with my dfs one by another, to get ten new dfs as an output.

Meaning something like this:

#place your elegant code here

df.a.new = df.a %>% do this %>% do that
df.b.new = df.b % do this %>% do that

#and so on

Edit:

#this should serve as a minimal reproducible code
df.a = c(1,2,3)
df.b = c(4,5,6)
df.c = c(7,8,9)

df.a.new = df.a %>% left_join(df.b)
Sphenoidale
  • 119
  • 6
  • If you can provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with code, sample data, and what you've tried, you will likely get a better response. However, sounds like you just want a function, `example_function <- function(df) df %>% ...` – caldwellst Jan 03 '22 at 21:23
  • 2
    You can add your 10 dos to a list and then pass that list to `lapply()` and get a list back with your 10 new data frames. – Dave2e Jan 03 '22 at 21:24
  • 2
    [List of frames](https://stackoverflow.com/a/24376207/3358227), along Dave2e's comment. – r2evans Jan 03 '22 at 21:30
  • @caldwellst is it better now? – Sphenoidale Jan 04 '22 at 06:09
  • @Dave2e yes, I was thinking about something like that, but does not know how to use lapply() in my case – Sphenoidale Jan 04 '22 at 06:10
  • Perhaps something like this? `names(.GlobalEnv)[grepl('df.', names(.GlobalEnv))] |> lapply(function(x) get(x) |> max())` – jpdugo17 Jan 04 '22 at 07:00

0 Answers0