Amelia
Amelia

Reputation: 29

How to iterate with the get() function in R

I have a few data frames that have the names df_JANUARY 2020, df_FEBRUARY 2020 etc. (I know spaces are an ill practice in variable assignment, but it has to do with a sql query). And would like to build a function to iterate through the months of these data frames. The purpose of this is have the function (not written below) clean each df the same way.

date <- c("JANUARY 2020", "FEBRUARY 2020") 

x <- function(date) {
     y <- get(paste0("df_", date))
} 

for(i in seq_along(date)) {
  z <- date[i]
  assign(paste0("dfclean_", date[i]), x(z))
}

The problem being that when I use the get() function it's pushing the whole list through rather than one element at a time. Is there away to avoid this problem with this methodology or is there a better way to approach this problem? Any help is extremely appreciated.

Upvotes: 1

Views: 105

Answers (2)

akrun
akrun

Reputation: 887831

We can convert the matrix to data.frame and then use $ as matrix columns are extracted with [

 x <- function(daten) {
      y <- as.data.frame(get(paste0("df_", daten)))
      y[grep("Enterprise", y$AcctType), ]

   } 


for(i in seq_along(date)) {
   z <- date[i]
     assign(paste0("dfclean_", date[i]), x(z))
  }

We can also use mget

lst1 <- mget(paste0("df_", date))
lst1 <- lapply(lst1, function(x) subset(as.data.frame(x), 
              grepl("Enterprise",AcctType)))
names(lst1) <- sub("_", "clean_", names(lst1))
list2env(lst1, .GlobalEnv)

Upvotes: 2

Ian Campbell
Ian Campbell

Reputation: 24878

I know you didn't ask for this, but how about just rename all of the dataframes with _ instead of space?

The first line assigns all of the objects in the global environment with df in the name to be elements of a list named mydfs.

The second line replaces space with _ in the names.

The third line assigns all of the list elements into the global environment.

mydfs <- mget(ls(pattern = "df"), globalenv())
names(mydfs) <- gsub(" ","_",names(mydfs))
list2env(mydfs, env = globalenv())

Or, option two, you could just use lapply on mydfs.

Upvotes: 1

Related Questions