[R] Fail to save an object using name as string

Marc Girondot m@rc_grt @ending from y@hoo@fr
Tue Apr 10 06:11:49 CEST 2018


Dear list member,

I think that I have detected a strange behavior of the save() command:

 > year <- "2000"
 > assign(paste0("Var_", year), list(A=10, B=20))
 > get(paste0("Var_", year))
$A
[1] 10

$B
[1] 20

# At this point all is ok, I have created a list of name Var_2000

 > save(paste0("Var_", year), file=paste0("Var_", year, ".Rdata"))
Error in save(paste0("Var_", year), file = paste0("Var_", year, 
".Rdata")) :
   objet ‘paste0("Var_", year)’ introuvable

(introuvable=cannot be found)

but

 > save("Var_2000", file=paste0("Var_", year, ".Rdata"))

When I read the help for save: ?save

...    the names of the objects to be saved (as symbols or character 
strings).

I checked if paste0("Var_", year) produced a character string:

 > str("Var_2000")
  chr "Var_2000"
 > str(paste0("Var_", year))
  chr "Var_2000"

The solution was to include the name in the list argument:

 > save(list=paste0("Var_", year), file=paste0("Var_", year, ".Rdata"))

But I don't understand what is the difference between "Var_2000" and 
paste0("Var_", year) as both produced the same output.

Thanks for your help,

Marc


R Under development (unstable) (2018-03-23 r74448) -- "Unsuffered 
Consequences"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)




More information about the R-help mailing list