Vaibhav
Vaibhav

Reputation: 338

How to create a dataframe whose name is stored in a vector

I have to run a R script every month, it read a .csv file into a dataframe and perform some manipulations on it.

The name of this dataframe needs to be dynamic for example: df_jan for January, df_feb for February and so on

I have created a character vector which contains the required data frame name using paste() function and Sys.Date() function

I want to automate this code therefore I don't want to rename this dataframe everytime I run this script

Now, how do I read the .csv into this data frame. Currently I'm loading the file into a dataframe - 'df' and using the assign() function to assign it the required name, Is there any better method to accomplish the same?

Thanks

Upvotes: 0

Views: 383

Answers (1)

utubun
utubun

Reputation: 4520

create.df <- function(path){
        assign(paste0("df_", format(Sys.Date(), "%b")),
               read.csv(path),
               envir = .GlobalEnv
               )
}

then call create.df with path to your .csv

Upvotes: 1

Related Questions