0

I have a parameter called country_name that reflects the name of a country I am interested in and which I change sometimes when I run my code, I would like my RDA file to reflect that name change once saved and loaded back into the environment.

Currently what happens is this:

Name the country:

country_name <- "Ireland"

Create a simple data frame:

x <- 10

vars_2_keep <- data.frame(x)

vars_2_keep

It contains x=10

x
1 10

I save it, renaming the data frame with the country name so that when I do this with a different country I will have country specific information:

save(vars_2_keep, file=paste("my_data_", country_name[1], ".rda", sep = ""))

I delete everything, and load it back in:

rm(list=ls())

load(file='my_data_Ireland.rda')

Unfortunately, in the environment instead of my data frame being called "my_data_Ireland", it is still called vars_2_keep.

How can I update the name of this data frame to my_data_country_name[1] (which in this example would be my_data_Ireland)

Thank you

  • The object you saved is recovered. The filename is a separate thing – cory Jan 23 '23 at 21:05
  • 1
    `assign(paste0("my_data_", country_name[1]), vars_2_keep)` will put `vars_2_keep` into an object named `my_data_Ireland`, which you could then save as an RDA file of any name -- it will still load as "my_data_Ireland". – Jon Spring Jan 23 '23 at 21:06
  • 4
    Preserving the variable names is the whole point of `save` and `load`. If you want to store *data* instead of variables, use `readRDS` and `saveRDS` instead. In addition, rather than making your variable names *variable*, you should almost certainly use a named list. – Konrad Rudolph Jan 23 '23 at 21:06
  • `save` retains the object name as you provide as arguments to it. If you want `load(file="..rda")` to retrieve an object named `Ireland`, then you need to do something like `Ireland <- vars_2_keep; save(Ireland, file=...)`. But I'm generally a bigger fan of using `saveRDS`/`readRDS` as KonradRudolph just suggested, with the consequence that you don't have the context of the object name, perhaps not _knowing_ that `data.frame(x=10)` refers to `Ireland` somehow. – r2evans Jan 23 '23 at 21:06
  • @KonradRudolph could you provide an example of doing this via `readRDS` and `saveRDS`? – James Moore Jan 23 '23 at 21:17
  • JamesMoore, it's as simple as `?readRDS` suggests: programmatically, you can do `saveRDS(setNames(list(vars_2_keep), country_name), file=sprintf("data_%s.rds", country_name))`, and then `X <- readRDS(sprintf("data_%s.rds", country_name))` followed by `X$Ireland` to get to your frame. Or `X <- readRDS(sprintf(..))$Ireland` and `X` is your frame. Note that you cannot just `readRDS(..)` without storing into an object, it does not create named objects in your environment. – r2evans Jan 23 '23 at 22:29
  • Or perhaps you can just do `saveRDS(vars_2_keep, file=sprintf("data_%s.rds", country_name))`, and then later to `X <- readRDS(sprintf(..))` and have `X` be your frame (though it won't innately have `Ireland` in its name). Of course, with all of this, you can easily just do `Ireland <- readRDS(..)` to get _that_. – r2evans Jan 23 '23 at 22:30
  • @JonSpring I like your suggestion but when I go to save my .rda I end up in the same boat, because I want to save not as "my_data_Ireland" but as "my_data_which ever country is under country_name". After applying your `assign(paste0("my_data_", country_name[1]), vars_2_keep)` how can I update my .rda save: `save(my_data_Ireland, file=paste("my_data_", country_name[1], ".rda", sep = ""))` such that it is saving as: `save(my_data_", country_name[1], file=paste("my_data_", country_name[1], ".rda", sep = ""))` – James Moore Jan 24 '23 at 11:52

0 Answers0