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

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

问题描述

这篇文章(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

这也是这个(http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f) R-faq 说应该避免这种情况.

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 的使用似乎被来自具有全局变量的语言的人采用,作为避免采用真正的 R 方式"的一种方式,即使用数据对象的函数式编程.他们真的应该学习使用列表,而不是在他们的工作区中放置单独命名的项目.

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 [.

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

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