TheWalküre
TheWalküre

Reputation: 45

r - Call multiple consecutive dataframes from global environment

I have multiple df in the global environment that follow this pattern

dfA.1
dfA.2
.
.
.
dfA.n

and

dfB.1
dfB.2
.
.
.
dfB.n

I need to apply a function to each pair of dfA and dfB:

repeat {
  A <- dfA.n
  B <- dfB.n
  
  
  command(A, B), verbose = TRUE))
  }

How do I create a loop to call the df pairs one by one so that the command applies to each pair of df (e.g. dfA.1 dfB.1) and then move on to the next?

Thank you for your help. I don't even know where to start.

Upvotes: 1

Views: 37

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389335

Use ls with pattern to get all the objects from global environment that follow a specific pattern. Use mget to get them in a list and with Map you can apply a function to each pair of variables.

Map(function(A, B) {apply the function here}, mget(ls(pattern = 'dfA')), 
                                              mget(ls(pattern = 'dfB')))

Upvotes: 3

Related Questions