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

。_饼干妹妹 提交于 2019-12-22 04:11:54

问题


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

回答1:


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`



回答2:


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.



来源:https://stackoverflow.com/questions/5021089/loading-someone-elses-rdata-file-cant-access-the-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!