Wilduck
Wilduck

Reputation: 14136

Loading someone else's .rdata file, can't access the data

My professor has sent me an .rdata file and wants me to do some analysis on the contents. Although I'm decent with R, I've never saved my work in .rdata files, and consequently haven't ever worked with them.

When I try to load the file, it looks like it's working:

> load('/home/swansone/Desktop/anes.rdata')
> ls()
[1] "25383-0001-Data"

But I can't seem to get at the data:

> names("25383-0001-Data")
NULL

I know that there is data in the .rdata file (it's 13 MB, there's definitely a lot in there) Am I doing something wrong? I'm at a loss.

Edit:

I should note, I've also tried not using quotes:

> names(25383-0001-Data)
Error: object "Data" not found

And renaming:

> ls()[1] <- 'nes'
Error in ls()[1] <- "nes" : invalid (NULL) left side of assignment

Upvotes: 9

Views: 10448

Answers (2)

Sacha Epskamp
Sacha Epskamp

Reputation: 47632

Maybe it has to do with the unusual use of dashes in the name and backquotes work:

names(`25383-0001-Data`)

Edit:

More for reference (since Joshua already answered the main question perfectly), you can also reassign an object from ls() (what Wilduck tried in the question) using get(). This might be useful if the object of the name contains very weird characters:

foo <- 1:5
bar <- get(ls()[1])
bar
[1] 1 2 3 4 5

This of course requires the index of foo in ls() to be [1], but looking up the index of the required object is not too hard.

Upvotes: 5

Joshua Ulrich
Joshua Ulrich

Reputation: 176718

You're going to run into a lot of issues with an object that doesn't begin with a letter or . and a letter (as mentioned in An Introduction to R).

Use backticks to access this object (the "Names and Identifiers" section of help("`") explains why this works) and assign the object to a new, syntactically validly named object.

Data <- `25383-0001-Data`

Upvotes: 16

Related Questions