[R] loading multiple .Rdata and preserving variable names

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Jul 15 12:12:14 CEST 2009


On Wed, Jul 15, 2009 at 10:41 AM, Duncan Murdoch<murdoch at stats.uwo.ca> wrote:

> Given two .RData files, you can load them into separate environments.
> Dealing with conflicting variables names is another issue, of course.
>
> For example:
>
> DataSet1 <- new.env()
> load( "somefile", envir=DataSet1)
>
> Now use ls(DataSet1) to see what you've got, repeat for DataSet2, etc. You
> can get a variable as DataSet1$VariableA.

 As an alternative to loading you can attach the RData and get
variables using "get".

 Create a couple of RData files with an 'x' in them:

 > x=1
 > save(x,file="x1.RData")
 > x=2
 > save(x,file="x2.RData")
 > q()

 Now in a new R session:

 > attach("x1.RData",warn=FALSE)
 > attach("x2.RData",warn=FALSE)
 > get("x",pos="file:x1.RData")
 [1] 1
 > get("x",pos="file:x2.RData")
 [1] 2

 I've used warn=FALSE to suppress the warnings that attach() gives you
when objects attached mask other objects of the same name.

 You can also detach() these things once you're done with them.

Barry




More information about the R-help mailing list