R中的' = '和'

时间:2022-01-11 13:20:44

Possible Duplicate:
Assignment operators in R: '=' and '<-'

可能重复:R: '='和'<-'中的赋值操作符

I'm using R 2.8.1 and it is possible to use both = and <- as variable assignment operators. What's the difference between them? Which one should I use?

我使用的是R 2。8.1,可以同时使用=和<-作为变量赋值运算符。它们之间有什么区别?我应该用哪个?

2 个解决方案

#1


52  

From here:

从这里开始:

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.

运算符<- and =赋值到计算它们的环境中。运算符<-可以在任何地方使用,而运算符=只允许在顶层(例如,在命令提示符键入的完整表达式中)或作为一个带支撑的表达式列表中的子表达式。

#2


13  

Reading from "Introducing Monte Carlo Methods with R", by Robert and Casella:

阅读罗伯特和卡塞拉的《用R介绍蒙特卡洛方法》:

"The assignment operator is =, not to be confused with ==, which is the Boolean operator for equality. An older assignment operator is <- and, for compatibility reasons, it still remains functional, but it should be ignored to ensure cleaner programming. (As pointed out by Spector, P. (2009). 'Data Manipulation with R' - Section 8.7., an exception is when using system.time, since = is then used to identify keywords)

赋值运算符是=,而不是==,这是布尔运算符的等式。一个旧的赋值操作符是<-并且,由于兼容性的原因,它仍然是功能性的,但是应该忽略它以确保更干净的编程。(正如Spector所指出的,p(2009)。“使用R进行数据处理”-第8.7节。在使用系统时,会出现例外。时间,因为=然后被用来识别关键字)

A misleading feature of the assignment operator <- is found in Boolean expressions such as

赋值操作符<-的一个误导特性可以在布尔表达式中找到,例如

> if (x[1]<-2) ...

which is supposed to test whether or not x[1] is less than -2 but ends up allocating 2 to x[1], erasing its current value! Note also that using

它被用来测试x[1]是否小于-2,但最终分配2到x[1],消除其当前值!也要注意使用

> if (x[1]=-2) ...

mistakenly instead of (x[1]==-2) has the same consequence."

而不是(x[1]= -2)也有同样的结果。

#1


52  

From here:

从这里开始:

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.

运算符<- and =赋值到计算它们的环境中。运算符<-可以在任何地方使用,而运算符=只允许在顶层(例如,在命令提示符键入的完整表达式中)或作为一个带支撑的表达式列表中的子表达式。

#2


13  

Reading from "Introducing Monte Carlo Methods with R", by Robert and Casella:

阅读罗伯特和卡塞拉的《用R介绍蒙特卡洛方法》:

"The assignment operator is =, not to be confused with ==, which is the Boolean operator for equality. An older assignment operator is <- and, for compatibility reasons, it still remains functional, but it should be ignored to ensure cleaner programming. (As pointed out by Spector, P. (2009). 'Data Manipulation with R' - Section 8.7., an exception is when using system.time, since = is then used to identify keywords)

赋值运算符是=,而不是==,这是布尔运算符的等式。一个旧的赋值操作符是<-并且,由于兼容性的原因,它仍然是功能性的,但是应该忽略它以确保更干净的编程。(正如Spector所指出的,p(2009)。“使用R进行数据处理”-第8.7节。在使用系统时,会出现例外。时间,因为=然后被用来识别关键字)

A misleading feature of the assignment operator <- is found in Boolean expressions such as

赋值操作符<-的一个误导特性可以在布尔表达式中找到,例如

> if (x[1]<-2) ...

which is supposed to test whether or not x[1] is less than -2 but ends up allocating 2 to x[1], erasing its current value! Note also that using

它被用来测试x[1]是否小于-2,但最终分配2到x[1],消除其当前值!也要注意使用

> if (x[1]=-2) ...

mistakenly instead of (x[1]==-2) has the same consequence."

而不是(x[1]= -2)也有同样的结果。