user1021713
user1021713

Reputation: 2203

Store a variable directly from loading in r

I have an RData "E.g.RData" I loaded it into R console using the load function.

load("E.g.RData")

it has a variable e.g. in RData. I am doing like this -

e <- load("E.g.RData")

then e gets the character vector as "e.g." but I want the contents of e.g. into e.

Is there a way to do it in R?

Upvotes: 2

Views: 209

Answers (3)

Greg Snow
Greg Snow

Reputation: 49650

Rather than using the load function with its defaults, which overwrites anything of the same name in the global workspace, you may prefer to use attach to attach the workspace, then copy just the object(s) of interest with the names you want, then detach the workspace.

Upvotes: 1

Marc in the box
Marc in the box

Reputation: 12005

Yeah, the problem is that E.g maintains its name during the saving of the object. You could try assigning the new name "e" to the E.g. object and then remove the E.g. object:

E.g <- runif(100)
save(E.g, file="E.g.Rdata")
load("E.g.Rdata")
assign("e", E.g)
rm(E.g)

Upvotes: 3

user1021713
user1021713

Reputation: 2203

This can be done using:

y <- get(load("path/E.g.RData"))

y will contain the contents of the e.g. variable.

Upvotes: 3

Related Questions