Skip to content Skip to sidebar Skip to footer

How To Query Tables With For Loop In R?

I have a vector with table names: table_names <- c('table1', 'table2', 'table3', 'table4') I want to query all this tables with limit 100 using for loop like this: for (val in

Solution 1:

It's often best to use lapply and keep the frames in a list (see https://stackoverflow.com/a/24376207/3358227):

allfour <- lapply(setNames(paste("select * from", table_names, "limit 100"),
                           table_names),
                  DBI::dbGetQuery, conn = con)

If you don't want them in a list, then you can assign them to an environment with list2env, such as

list2env(allfour, envir = .GlobalEnv)

Post a Comment for "How To Query Tables With For Loop In R?"