Reputation: 15458
I am trying to save the stata
data (data.dta
) as R
data and then load the data using the paste
function and then assign the name using get
and paste
function.
library(foreign)
getwd()
"C:/Users/Vista/Documents/project"
year<-2010
income2010x.dta<-read.dta("data.dta")
save(income2010x.dta,file="income2010x.rda")
load( paste0( "income" , year , "x.rda" ) )
z <- get( paste0( "income" , year , "x.dta" ) ) # works
z <- get( paste0( "income" , year , "x.rda" ) ) # doesn't work
z
Error in get(income2010x.rda) : object 'income2010x.rda' not found
ls()
income2010x.dta
...
I find that income2010x.dta
is stored as the object after these steps but income2010x.rda
is not. So, z <- get( paste0( "income" , year , "x.rda" ) )
didn't work. Any idea?
Upvotes: 1
Views: 288
Reputation: 263362
When you do this:
load( paste0( "income" , year , "x.rda" ) )
.... You get (in the colloquial sense of "acquire" or "access" or "recover") an object (or objects) named whatever named it had when it (they) were saved. In this case you would have an object named 'income2010x.dta' but no object named "income2010x.rda", so this "get" in the restricted meaning of the R function:
z <- get( paste0( "income" , year , "x.rda" ) )
.... as you observed ..."doesn't work".
Upvotes: 1
Reputation: 115390
income2010x.dta<-read.dta("data.dta")
creates a data.frame call ed income2010x.dta
within the workspace.
save
allows you to save many objects, and when you load
them they will have the same names as when you saved them.
you can use saveRDS
to save a single object and readRDS
to read it to another name
saveRDS(income2010x.dta,file="income2010x.rda")
income2010x.rda <- readRDS('income2010x.rda')
That being said, I don't understand why you need two copies, or why you are using get
to create a third copy of the same object as z
Upvotes: 1