加载别人的.rdata文件,无法访问数据
我的教授给我发送了一个 .rdata 文件,并希望我对内容进行一些分析。虽然我对 R 很熟悉,但我从未将我的工作保存在 .rdata 文件中,因此从未使用过它们。
当我尝试加载该文件时,它看起来正在工作:
> load('/home/swansone/Desktop/anes.rdata')
> ls()
[1] "25383-0001-Data"
但我似乎无法获取数据:
> names("25383-0001-Data")
NULL
我知道 .rdata 文件中有数据(它有 13 MB,里面肯定有很多数据)我做错了什么吗?我不知所措。
编辑:
我应该注意,我也尝试过不使用引号:
> names(25383-0001-Data)
Error: object "Data" not found
并重命名:
> ls()[1] <- 'nes'
Error in ls()[1] <- "nes" : invalid (NULL) left side of assignment
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于不以字母或 开头的对象,您将会遇到很多问题。和一封信(如中所述R 简介)。
使用反引号访问此对象(
help("`")
的“名称和标识符”部分解释了其工作原理)并将该对象分配给一个新的、语法上有效命名的对象。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.也许这与名称和反引号工作中不寻常地使用破折号有关:
编辑:
更多参考(因为 Joshua 已经完美回答了主要问题),您还可以从 ls() 重新分配一个对象 (Wilduck 在问题中尝试过的)使用
get()
。如果名称的对象包含非常奇怪的字符,这可能很有用:这当然要求
ls()
中foo
的索引为[1]< /code>,但是查找所需对象的索引并不太难。
Maybe it has to do with the unusual use of dashes in the name and backquotes work:
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) usingget()
. This might be useful if the object of the name contains very weird characters:This of course requires the index of
foo
inls()
to be[1]
, but looking up the index of the required object is not too hard.