为什么使用分配不好? [英] Why is using assign bad?

查看:94
本文介绍了为什么使用分配不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章( R中的惰性评估–分配会受到影响吗?)涵盖了一些共同点,但我不确定它能否回答我的问题.

This post (Lazy evaluation in R – is assign affected?) covers some common ground but I am not sure it answers my question.

很久以前,我发现apply家族时就停止使用assign了,尽管如此,纯粹是出于优雅的原因,例如:

I stopped using assign when I discovered the apply family quite a while back, albeit, purely for reasons of elegance in situations such as this:

names.foo <- letters
values.foo <- LETTERS
for (i in 1:length(names.foo))
  assign(names.foo[i], paste("This is: ", values.foo[i]))

可以替换为:

foo <- lapply(X=values.foo, FUN=function (k) paste("This is :", k))
names(foo) <- names.foo

这也是此原因(

This is also the reason this (http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f) R-faq says this should be avoided.

现在,我知道assign通常不被接受.但是还有其他我不知道的原因吗?我怀疑这可能会影响范围界定或懒惰的评估,但我不确定吗?演示此类问题的示例代码将非常有用.

Now, I know that assign is generally frowned upon. But are there other reasons I don't know? I suspect it may mess with the scoping or lazy evaluation but I am not sure? Example code that demonstrates such problems will be great.

推荐答案

实际上,这两个操作是完全不同的.第一个给您26个不同的对象,而第二个仅给您一个.第二个对象将在分析中更容易使用.因此,我想我想您已经证明了assign的主要缺点,即必须始终使用get来收集或收集所有现在在全球范围内松散"的相似命名的单个对象的必要性.环境.尝试想象如何使用这26个单独的对象依次执行任何操作.一个简单的lapply(foo, func)就足以满足第二种策略.

Actually those two operations are quite different. The first gives you 26 different objects while the second gives you only one. The second object will be a lot easier to use in analyses. So I guess I would say you have already demonstrated the major downside of assign, namely the necessity of then needing always to use get for corralling or gathering up all the similarly named individual objects that are now "loose" in the global environment. Try imagining how you would serially do anything with those 26 separate objects. A simple lapply(foo, func) will suffice for the second strategy.

该FAQ引用实际上仅表示使用赋值然后分配名称更容易,但并不表示它是不好的".我碰巧将其读取为功能欠佳",因为您实际上并未返回要分配的值.效果似乎是副作用(在这种情况下,assign策略会导致26种单独的副作用). assign的使用似乎已被来自具有全局变量的语言的人们所采用,以避免使用"True R Way",即使用数据对象进行函数式编程.他们确实应该学习使用列表,而不是用单独命名的项目乱扔他们的工作空间.

That FAQ citation really only says that using assignment and then assigning names is easier, but did not imply it was "bad". I happen to read it as "less functional" since you are not actually returning a value that gets assigned. The effect looks to be a side-effect (and in this case the assign strategy results in 26 separate side-effects). The use of assign seems to be adopted by people that are coming from languages that have global variables as a way of avoiding picking up the "True R Way", i.e. functional programming with data-objects. They really should be learning to use lists rather than littering their workspace with individually-named items.

还有另一种分配范式可以使用:

There is another assignment paradigm that can be used:

 foo <- setNames(  paste0(letters,1:26),  LETTERS)

这将创建一个命名的原子向量,而不是一个命名列表,但是仍然可以使用赋予[的名称来访问向量中的值.

That creates a named atomic vector rather than a named list, but the access to values in the vector is still done with names given to [.

这篇关于为什么使用分配不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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