我应该养成删除 R 中未使用变量的习惯吗? [英] Should I get a habit of removing unused variables in R?

查看:42
本文介绍了我应该养成删除 R 中未使用变量的习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在处理相对较大的数据文件,而且我的计算机不是超级计算机.我正在临时创建这些数据集的许多子集,并且不会将它们从工作区中删除.显然,这些使许多变量变得混乱.但是,有许多未使用的变量对 R 的性能有什么影响吗?(即计算机的内存是否会在某个时候填满?)
在编写代码时,我应该养成删除未使用变量的习惯吗?值得吗?

Currently I'm working with relatively large data files, and my computer is not a super computer. I'm creating many subsets of these data sets temporarily and don't remove them from workspace. Obviously those are making a clutter of many variables. But, is there any effect of having many unused variables on performance of R? (i.e. does memory of computer fill at some point?)
When writing code should I start a habit of removing unused variables? Does it worth it?

x <- rnorm(1e8)
y <- mean(x)
# After this point I will not use x anymore, but I will use y
# Should I add following line to my code? or 
#   Maybe there will not be any performance lag if I skip the following line:
rm(x)

我不想在我的代码中添加另一行.与其让我的代码看起来杂乱无章,我更喜欢我的工作区杂乱无章(如果不会有性能改进).

I don't want to add another line to my code. Instead of my code to seem cluttered I prefer my workspace to be cluttered (if there will be no performance improvement).

推荐答案

是的,拥有未使用的对象会影响您的性能,因为 R 将其所有对象存储在内存中.显然,小对象的影响可以忽略不计,您通常只需要删除真正大的对象(具有数百万行的数据框等),但拥有整洁的工作区不会有任何影响.

Yes, having unused objects will affect your performance, since R stores all its objects in memry. Obviously small objects will have negligible impact, and you mostly need to remove only the really big ones (data frames with millions of rows, etc) but having an uncluttered workspace won't hurt anything.

唯一的风险是删除您以后需要的东西.即使按照建议使用存储库,您也希望避免意外破坏内容.

The only risk is removing something that you need later. Even when using a repo, as suggested, breaking stuff accidentally is something you want to avoid.

解决这些问题的一种方法是广泛使用local.当你做一个散布在许多临时对象周围的计算时,你可以将它包装在一个 local 调用中,它会在之后有效地为你处理这些对象.不再需要清理大量的 ijxtemp.var 等等.

One way to get around these issues is to make extensive use of local. When you do a computation that scatters around lots of temporary objects, you can wrap it inside a local call, which will effectively dispose of those objects for you afterward. No more having to clean up lots of i, j, x, temp.var, and whatnot.

local({
    x <- something
    for(i in seq_along(obj))
        temp <- some_unvectorised function(obj[[i]], x)
        for(j in 1:temp)
            temp2 <- some_other_unvectorised_function(temp, j)
    # x, i, j, temp, temp2 only exist for the duration of local(...)
})

这篇关于我应该养成删除 R 中未使用变量的习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆