Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create file paths based on variable content

I would like to save different data frames in R that is titled with a year. My approach so far has been to do like this:

year <- 2020
write.table(df, file = "D:/.../.../df_year.xlsx")

If I am using the above approach, I am ending with a data frame called df_year.xlsx, but I want it to be named "df_2020.xlsx".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is the glue package (Part of the tidyverse) for this:

library(stringr)
year <- 2020
write.table(df, file = str_glue("D:/.../.../df_{year}.xlsx"))

Alternatively, one can use paste:

write.table(df, file = paste0("D:/.../.../df_", year, ".xlsx"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading