楼主: daxia101
69528 16

[学习分享] 在R中,赋值语句<-, <<-, =有什么区别 [推广有奖]

11
千载之后 发表于 2014-8-8 06:54:16
qoiqpwqr 发表于 2011-11-14 09:51
有的时候可以用 -> 进行赋值而 = 不行
比如想复制一个式子的值给变量x
我经常先输入了一堆的式子,然后突 ...
非常感谢!

12
nml 发表于 2014-8-16 12:02:43
The operators <- and = assign into the environment in which they are evaluated. The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

The operators <<- and ->> are normally only used in functions, and cause a search to made through parent environments for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment. Note that their semantics differ from that in the S language, but are useful in conjunction with the scoping rules of R. See ‘The R Language Definition’ manual for further details and examples.
简单概括:
1.<-或->可赋值到当前操作的环境里的变量
2.<<-或->>一般用于函数内部,然后会搜索包括父层级在内的被定义的变量然后赋值.

13
daxia101 发表于 2014-8-17 00:10:42
I see, thank you!

14
fan19889017 发表于 2014-8-25 14:41:15
赋值成功:tcpdpl_source <- transform(tcpdpl_source,tcpdpl_cur_base_mney = tcp_cur_base_mney + dpl_cur_base_mney + 0)

赋值失败:tcpdpl_source <- transform(tcpdpl_source,tcpdpl_cur_base_mney <- tcp_cur_base_mney + dpl_cur_base_mney + 0)

15
夏日沧海 发表于 2014-12-8 19:53:00
很清楚,但是这个用法的大量使用是很SB的,计算机语言符号应该尽量的能够融合,减少编程人员无谓的记忆和修改代码的麻烦,统一用=,什么事都没有。

16
18735115712 发表于 2015-12-1 19:03:49
谢谢!

17
ophui 发表于 2016-3-2 16:19:02
daxia101 发表于 2011-11-14 02:09
看例子:

> matrix(1:20,ncol ncol
这块的话应该和编译器有关,从语法上看,是先执行ncol <-4的代码后,并没有对matrix进行参数传递,所有等同于4

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 06:14