2

I have data.frame objects with normalized names into my global env and I want to save them into .Rda files.

My first question is, should I save them into one big .Rda file or should I create one file for each data frame ? (df have 14 col and ~260 000 row).

Assuming that I'll save them into differents files, I was thinking about a function like this : (All my data.frame names begin by "errDatas")

sapply(ls(pattern = "errDatas"), function(x) save(as.name(x), file = paste0(x, ".Rda")))

But I have this error :

Error in save(as.name(x), file = paste0(x, ".Rda")) : objet ‘as.name(x)’ introuvable

Seems like save can't parse as.name(x) and evaluate it as is. I tried also with eval(parse(text = x)) but it's the same thing.

Do you have an idea about how I can manage to save my data frames within a loop ? Thanks.

And I have a bonus question to know if what I'm trying to do is useful and legit : These data frames come from csv files (one data frame by csv file which I import with read.csv). Each day I have one new csv file and I want to do some analysis on all the csv files. I realized that reading from csv is much slower than saving and loading a Rda file. So instead of reading all the csv each time I run my program, I actualy want to read each csv file only once, saving it into a Rda file and then loading it. Is this a good idea ? Is there best-practices for that with R ?

Julien Navarre
  • 7,653
  • 3
  • 42
  • 69

1 Answers1

3

Use the list= parameter of the save function. This allows you to specify the name of the object as a character vector rather than passing the object itself. For example

lapply(ls(pattern = "errDatas"), function(x) {
    save(list=x, file = paste0(x, ".Rda"))
})
MrFlick
  • 195,160
  • 17
  • 277
  • 295