oliyiyi
如:
x=c(1:100)
与
x<-c(1:100)
刚刚学习R,谢谢指教!
alexru
一样的
yihui
曾经"="是不能作为赋值符号的,不过现在不用管了,都一样。
另,1:100是错的,应该是1:100,而且c()也多余
oliyiyi
3q!!!!!!!!!
sociology
[quote]引用第1楼alexru于2007-05-31 22:27发表的“”:
一样的[/quote]
不完全一样,取决于具体情况。在楼主举例的情况是一样的。但是在我举例的情况下是不同的。
> rm(list=ls(all=TRUE))
> a <- mean(x<-1:10)
> a
[1] 5.5
> x
[1] 1 2 3 4 5 6 7 8 9 10
> rm(list=ls(all=TRUE))
> a <- mean(x=1:10)
> a
[1] 5.5
> x
Error: object "x" not found
oliyiyi
[quote]引用第4楼sociology于2007-06-01 09:50发表的“”:
不完全一样,取决于具体情况。在楼主举例的情况是一样的。但是在我举例的情况下是不同的。
> rm(list=ls(all=TRUE))
> a <- mean(x<-1:10)
.......[/quote]
<-更稳健吧?
为什么会这样呢,是不是R把=当做逻辑符了,好像也不是啊???
abel
scope的差别,另外还有相似的<<-运算符
yihui
=不是逻辑运算符,==才是。
neige
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.
From R docs
sminus
有区别!!!
wuguohui
恩,又学会了一点东西,感谢了啊
godventure
我糊涂了
judelc
[quote]引用第8楼neige于2007-06-01 21:44发表的“”:
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.
From R docs[/quote]
不是特别明白,谁能给稍微解释下么?
yihui
给两个例子就明白了:
比如plot(x<-1:10)就是在作图之前把1:10赋给x(这个expression在plot之前就被evaluate了也就是赋值了),所以作完图之后变量x也就有值了,而你用plot(x=1:10)就不能达到对x赋值的目的,作完图之后x这个对象仍然不存在。只有直接执行x=1:10才能达到对x赋值的目的。
> plot(x=1:10)
> x
错误: 找不到这个对象"x"
> plot(x<-1:10)
> x
[1] 1 2 3 4 5 6 7 8 9 10
总之就是<-几乎在任何地方都可以完成赋值的工作,而=只能在顶层环境下才能做赋值。