-2

I have 4 dataframes say A, B, C, D that have same columns.

I want to do something like:

for x in (A,B,C,D): do something

It means I want to perform the same task on each dataframe one by one. I tried:

for (x in c(A,B,C,D)) { do something }

but it does not work.

How could I do that in R?

Thank you very much

mommomonthewind
  • 3,057
  • 6
  • 31
  • 58
  • See [this post](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for a number of options. Gregor's answer there gives you some tips for working with such objects. – lmo Jun 02 '17 at 12:10

2 Answers2

1

You can make list of them, say:

X <- list (A, B, C, D)

and then use lapply or sapply:

lapply(X, function_doing_something)
Łukasz Deryło
  • 1,458
  • 1
  • 10
  • 26
1

Create a list of dataframes

x <- list(A, B, C, D)
for(i in 1:length(x)) {
your function here }

You'll be supposed to access each dataframe in the list as x[[i]]

Hope this helps!

Kalees Waran
  • 539
  • 5
  • 11