避免传递数据框的最佳方法是什么?

发布于 2024-07-14 11:23:06 字数 344 浏览 3 评论 0原文

我有 12 个 data.frame 需要使用。 它们很相似,我必须对每一个进行相同的处理,因此我编写了一个函数,该函数接受 data.frame,对其进行处理,然后返回一个 data.frame >。 这有效。 但我担心我正在绕过一个非常大的结构。 我可能正在制作临时副本(是吗?)这效率不高。 避免传递 data.frame 的最佳方法是什么?

doSomething <- function(df) {
  // do something with the data frame, df
  return(df)
}

I have 12 data.frames to work with. They are similar and I have to do the same processing to each one, so I wrote a function that takes a data.frame, processes it, and then returns a data.frame. This works. But I am afraid that I am passing around a very big structure. I may be making temporary copies (am I?) This can't be efficient. What is the best way to avoid passing a data.frame around?

doSomething <- function(df) {
  // do something with the data frame, df
  return(df)
}

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

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

发布评论

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

评论(3

猫瑾少女 2024-07-21 11:23:06

事实上,您正在传递对象并使用一些内存。 但我认为你不能在不传递对象的情况下对 R 中的对象进行操作。 即使您没有创建函数并在函数外部进行操作,R 的行为也基本相同。

看到这一点的最好方法是树立一个例子。 如果您使用的是 Windows,请打开 Windows 任务管理器。 如果您使用的是 Linux,请打开终端窗口并运行 top 命令。 在此示例中我将假设使用 Windows。 在 R 中运行以下命令:

col1<-rnorm(1000000,0,1)
col2<-rnorm(1000000,1,2)
myframe<-data.frame(col1,col2)

rm(col1)
rm(col2)
gc()

这会创建一对名为 col1 和 col2 的向量,然后将它们组合成一个名为 myframe 的数据框。 然后它会丢弃向量并强制运行垃圾收集。 在 Windows 任务管理器中观察 Rgui.exe 任务的内存使用情况。 当我启动 R 时,它使用大约 19 兆内存。 运行上述命令后,我的机器使用的 R 内存略低于 35 兆。

现在尝试一下:

myframe<-myframe+1

您的 R 内存使用量应跃升至超过 144 兆。 如果你使用 gc() 强制垃圾回收,你会发现它回落到 35 兆左右。 要使用函数尝试此操作,您可以执行以下操作:

doSomething <- function(df) {
    df<-df+1-1
return(df)
}
myframe<-doSomething(myframe)

当您运行上面的代码时,内存使用量将跃升至 160 meg 左右。 运行 gc() 会将其降至 35 兆。

那么这一切该怎么办呢? 好吧,在函数外部执行操作并不比在函数中执行操作更有效(就内存而言)。 垃圾收集确实很好地清理了东西。 你应该强制 gc() 运行吗? 可能不会,因为它会根据需要自动运行,我只是在上面运行它以显示它如何影响内存使用。

我希望这有帮助!

You are, indeed, passing the object around and using some memory. But I don't think you can do an operation on an object in R without passing the object around. Even if you didn't create a function and did your operations outside of the function, R would behave basically the same.

The best way to see this is to set up an example. If you are in Windows open Windows Task Manager. If you are in Linux open a terminal window and run the top command. I'm going to assume Windows in this example. In R run the following:

col1<-rnorm(1000000,0,1)
col2<-rnorm(1000000,1,2)
myframe<-data.frame(col1,col2)

rm(col1)
rm(col2)
gc()

this creates a couple of vectors called col1 and col2 then combines them into a data frame called myframe. It then drops the vectors and forces garbage collection to run. Watch in your windows task manager at the mem usage for the Rgui.exe task. When I start R it uses about 19 meg of mem. After I run the above commands my machine is using just under 35 meg for R.

Now try this:

myframe<-myframe+1

your memory usage for R should jump to over 144 meg. If you force garbage collection using gc() you will see it drop back to around 35 meg. To try this using a function, you can do the following:

doSomething <- function(df) {
    df<-df+1-1
return(df)
}
myframe<-doSomething(myframe)

when you run the code above, memory usage will jump up to 160 meg or so. Running gc() will drop it back to 35 meg.

So what to make of all this? Well, doing an operation outside of a function is not that much more efficient (in terms of memory) than doing it in a function. Garbage collection cleans things up real nice. Should you force gc() to run? Probably not as it will run automatically as needed, I just ran it above to show how it impacts memory usage.

I hope that helps!

猫腻 2024-07-21 11:23:06

我不是 R 专家,但大多数语言都对大对象使用引用计数方案。 在修改对象的副本之前,不会创建对象数据的副本。 如果您的函数仅读取数据(即用于分析),则不应进行复制。

I'm no R expert, but most languages use a reference counting scheme for big objects. A copy of the object data will not be made until you modify the copy of the object. If your functions only read the data (i.e. for analysis) then no copy should be made.

征﹌骨岁月お 2024-07-21 11:23:06

我在寻找其他东西时遇到了这个问题,而且它已经很旧了 - 所以我现在只提供一个简短的答案(如果您需要更多解释,请发表评论)。

您可以在 R 中传递包含 1 到所有变量的环境。 但也许你不需要担心它。

[您也许还可以对类执行类似的操作。 我目前只了解如何使用类来实现多态函数 - 并且注意有不止 1 个类系统在运行。]

I came across this question looking for something else, and it's old - so I'll just provide a brief answer for now (leave a comment if you'd like more explanation).

You can pass around environments in R which contain anywhere from 1 to all of your variables. But probably you don't need to worry about it.

[You might also be able to do something similar with classes. I only currently understand how to use classes for polymorphic functions - and note there's more than 1 class system kicking around.]

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