从 R 中加载 .rda 文件获取数据帧的名称

发布于 2024-12-21 20:50:09 字数 360 浏览 6 评论 0原文

我正在尝试在 r 中加载一个 .rda 文件,它是保存的数据帧。但我不记得它的名字了。

我已经尝试过

a<-load("al.rda")

,然后不允许我对 a 做任何事情。我收到错误

Error:object 'a' not found

我也尝试使用 = 符号。

如何加载此 .rda 文件以便使用它?

我用 load("al.rda) 重新启动 R,我知道出现以下错误

Error: C stack usage is too close to the limit

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

魂牵梦绕锁你心扉 2024-12-28 20:50:09

我有一个类似的问题,并且无需重新安装 R 即可解决。例如,执行

load("al.rda) 工作正常,但是如果您这样做
a <- load("al.rda") 将不起作用。

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.

只有影子陪我不离不弃 2024-12-28 20:50:09

使用“attach”,然后使用带有名称参数的“ls”。类似于:

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

数据文件现在很可能位于您的搜索路径上的位置 2。做:

search()
ls(pos=2)

为了开悟。现在,输入保存在 al.rda 中的任何对象的名称都可以得到它,除非您在搜索路径位置 1 中有某些内容,但 R 可能会警告您一些关于某个事物掩盖了另一个事物(如果有)的消息。

但我现在怀疑您在 RData 文件中没有保存任何内容。有两个原因:

  1. 您说您没有收到错误消息
  2. 加载说没有加载任何内容

我可以复制这种情况。如果您执行 save(file="foo.RData") 那么您将得到一个空的 RData 文件 - 您可能想要做的是 save.image(file="foo.RData") ,它保存所有对象。

你的这个.rda 文件有多大?如果它小于 100 字节(我的空 RData 文件有 42 字节长),那么我怀疑这就是发生的情况。

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.

维持三分热 2024-12-28 20:50:09

我不得不重新安装 R...不知何故它已损坏。我期望的简单命令

load("al.rda")

终于起作用了。

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

load("al.rda")

finally worked.

千里故人稀 2024-12-28 20:50:09

load 函数确实返回它加载的变量列表。我怀疑您在加载“al.rda”时实际上遇到了错误。加载时 R 到底输出什么?

应该如何工作的示例:

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

为了确保这一点,请检查您实际调用的load函数是否是原始函数:

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

编辑因为您现在加载文件时出现错误,它可能在某种程度上已损坏。尝试一下并说出它打印的内容:

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

如果无法访问该文件,则很难进行更多调查...也许您可以以某种方式共享该文件(http://www.filedropper.com 或类似的)?

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)?

篱下浅笙歌 2024-12-28 20:50:09

我通常使用 save 仅保存单个对象,然后使用以下实用程序方法使用 load 将该对象检索到给定变量名中,但检索到临时命名空间中以避免覆盖现有对象。也许它对其他人也有帮助:

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

该方法当然可以扩展为也返回命名对象和对象列表,但这个简单的版本对我来说是最有用的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文