从Lapply()获得临时结果

发布于 2025-02-05 13:42:54 字数 427 浏览 4 评论 0原文

我有一些lapply代码,需要很长时间才能计算。类似的事情:

n <- 10^7
res <- lapply(1:n, function(i){
  print(round(i/n*100, 0))
  rnorm(100)
  })

我们可以以某种方式保存临时结果,而回合(i/n*100,0)比100%少,即代码仍在运行?

我现在正在运行实际的lapply()代码,零件print(rough(i/n*100,0))告诉我,我的计算机几乎完成了(99%!)。问题是我需要关闭计算机,因为我离开了工作场所。在res&lt; - lapply(...)零件仍在运行时,是否有机会获得数据R的计算?真的不想让他明天再次计算这99%的年龄。

I have some lapply code which takes a really long while to compute. Something like this:

n <- 10^7
res <- lapply(1:n, function(i){
  print(round(i/n*100, 0))
  rnorm(100)
  })

Can we somehow save the interim results while round(i/n*100, 0) is less then 100%, i.e. the code is still running?

I am running the my actual lapply() code right now and the part print(round(i/n*100, 0)) tells me that my computer is almost done (99%!). The problem is that I need to turn off the computer because I leave my work place. Is there any chance to get the data R has calculated so far while the res <- lapply(...) part is still running? Really don`t want him to calculate for ages those 99% tomorrow again..

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

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

发布评论

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

评论(1

吖咩 2025-02-12 13:42:55

一旦lapply()代码运行,似乎无法捕获临时结果。但是,有两个选项

  • 使用循环的@gregorthomas建议
  • 使用&lt;&lt; -lapply(Lapply()函数中,确实保存了临时结果。

这是问题中代码的第二个选项:

n <- 10^7
res <- vector("list", n)
res <- lapply(1:n, function(i){
  print(round(i/n*100, 0))
  res[[i]] <<- rnorm(100)
})

现在我们可以查看并查看临时数据实际上保存了:

head(res)
tail(res)

Once the lapply() code runs there seems to be no way to catch the interim results. But there are two options

  • using a for loop as suggested by @GregorThomas
  • using <<- inside the lapply() function which does save interim results.

Here is the second option for the piece of code in the question:

n <- 10^7
res <- vector("list", n)
res <- lapply(1:n, function(i){
  print(round(i/n*100, 0))
  res[[i]] <<- rnorm(100)
})

Now we can have a look and see that the interim data is actually saved:

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