getting the name of a dataframe from loading a .rda file in R

爷,独闯天下 提交于 2020-01-23 06:18:34

问题


I am trying to load an .rda file in r which was a saved dataframe. I do not remember the name of it though.

I have tried

a<-load("al.rda")

which then does not let me do anything with a. I get the error

Error:object 'a' not found

I have also tried to use the = sign.

How do I load this .rda file so I can use it?

I restared R with load("al.rda) and I know get the following error

Error: C stack usage is too close to the limit

回答1:


Use 'attach' and then 'ls' with a name argument. Something like:

attach("al.rda")
ls("file:al.rda")

The data file is now on your search path in position 2, most likely. Do:

search()
ls(pos=2)

for enlightenment. Typing the name of any object saved in al.rda will now get it, unless you have something in search path position 1, but R will probably warn you with some message about a thing masking another thing if there is.

However I now suspect you've saved nothing in your RData file. Two reasons:

  1. You say you don't get an error message
  2. load says there's nothing loaded

I can duplicate this situation. If you do save(file="foo.RData") then you'll get an empty RData file - what you probably meant to do was save.image(file="foo.RData") which saves all your objects.

How big is this .rda file of yours? If its under 100 bytes (my empty RData files are 42 bytes long) then I suspect that's what's happened.




回答2:


The load function does return the list of variables that it loaded. I suspect you actually get an error when you load "al.rda". What exactly does R output when you load?

Example of how it should work:

d <- data.frame(a=11:13, b=letters[1:3])
save(d, file='foo.rda')
a <- load('foo.rda')
a # prints "d"

Just to be sure, check that the load function you actually call is the original one:

find("load") # should print "package:base"

EDIT Since you now get an error when you load the file, it is probably corrupt in some way. Try this and say what it prints:

file.info("a1.rda") # Prints the file size etc...
readBin("a1.rda", "raw", 50) # reads first 50 bytes from the file

Without having access to the file, it's hard to investigate more... Maybe you could share the file somehow (http://www.filedropper.com or similar)?




回答3:


I had to reinstall R...somehow it was corrupt. The simple command which I expected of

load("al.rda")

finally worked.




回答4:


I had a similar issue, and it was solved without reinstall R. for example doing

load("al.rda) works fine, however if you do a <- load("al.rda") will not work.




回答5:


I usually use save to save only a single object, and I then use the following utility method to retrieve that object into a given variable name using load, but into a temporary namespace to avoid overwriting existing objects. Maybe it will be helpful for others as well:

load_first_object <- function(fname){
    e <- new.env(parent = parent.frame())
    load(fname, e)
    return(e[[ls(e)[1]]])
}

The method can of course be extended to also return named objects and lists of objects, but this simple version is for me the most useful.



来源:https://stackoverflow.com/questions/8547919/getting-the-name-of-a-dataframe-from-loading-a-rda-file-in-r

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