Jeni
Jeni

Reputation: 968

Unquote elements from a vector of variable names R

I have a vector with some dataframe names:

myvector<-c('a', 'b', 'c', 'd')

Now, in a for loop, I would like to do some manipulation to each dataframe named with 'myvector' elements, so I would like to unquote its elements.

myvector2<-c(a, b, c, d)

Thanks!

Upvotes: 2

Views: 391

Answers (2)

akrun
akrun

Reputation: 887911

We can use map

library(purrr)
map(mget(myvector), ~ f1(.x))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389315

You could use get to get a single dataframe in a loop.

for (vec in myvector) {
     df <- get(vec)
     #Do some stuff
}

Or use mget to get list of dataframes and then apply some function with lapply.

lapply(mget(myvector), function(x) {
    #Do some stuff
})

Upvotes: 1

Related Questions