Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

load

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
like image 404
Wilduck Avatar asked Feb 16 '11 19:02

Wilduck


People also ask

How do I open a .RData file?

The easiest way to load the data into R is to double-click on the particular file yourfile. RData after you download it to your computer. This will open in RStudio only if you have associated the . RData files with RStudio.

What is a .RData file?

R also has two native data formats—Rdata (sometimes shortened to Rda) and Rds. These formats are used when R objects are saved for later use. Rdata is used to save multiple R objects, while Rds is used to save a single R object. See below for instructions on how to read and load data into R from both file extensions.

Where is .RData file saved?

RData file in the data folder of your working directory.

How do you load data into an R workspace?

Load workspace in R On the one hand, to load the RData object you can use the load function and call the file name. Once loaded, if you call the objects you saved you can access them. On the other hand, to read an RDS object you can use the readRDS function and specify the . rds file.


2 Answers

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`
like image 62
Joshua Ulrich Avatar answered Nov 11 '22 09:11

Joshua Ulrich


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.

like image 30
Sacha Epskamp Avatar answered Nov 11 '22 07:11

Sacha Epskamp