楼主: daxia101
69529 16

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

  • 3关注
  • 1粉丝

已卖:195份资源

副教授

88%

还不是VIP/贵宾

-

威望
0
论坛币
4432 个
通用积分
68.3346
学术水平
0 点
热心指数
6 点
信用等级
0 点
经验
131572 点
帖子
213
精华
0
在线时间
1885 小时
注册时间
2006-7-12
最后登录
2025-8-25

楼主
daxia101 发表于 2011-11-14 00:58:26 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
初学R,请问在R中,赋值语句<-, <<-, =有什么区别?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝


回帖推荐

Weitaiyun 发表于10楼  查看完整内容

= 是之前参数传递,现在也可以赋值可,但和

沙发
daxia101 发表于 2011-11-14 01:50:06
What’s the difference?

The main difference between the two assignment operators is scope. It’s easiest to see the difference with an example:

##Delete x (if it exists)
> rm(x)
> mean(x=1:10) #[1] 5.5
> x #Error: object 'x' not found

Here x is declared within the function’s scope of the function, so it doesn’t exist in the user workspace. Now, let’s run the same piece of code with using the <- operator:

> mean(x <- 1:10)# [1] 5.5
> x # [1] 1 2 3 4 5 6 7 8 9 10

This time the x variable is declared within the user workspace.

When does the assignment take place?

In the code above, you may be tempted to thing that we “assign 1:10 to x, then calculate the mean.” This would be true for languages such as C, but it isn’t true in R. Consider the following function:

> a <- 1
> f <- function(a) return(TRUE)
> f <- f(a <- a + 1); a
[1] TRUE
[1] 1

Notice that the value of a hasn’t changed! In R, the value of a will only change if we need to evaluate the argument in the function. This can lead to unpredictable behaviour:

> f <- function(a) if(runif(1)>0.5) TRUE else a
> f(a <- a+1);a
[1] 2
> f(a <- a+1);a
[1] TRUE
[1] 2
> f(a <- a+1);a
[1] 3

藤椅
daxia101 发表于 2011-11-14 01:50:52
还是不完全理解

板凳
iyiyilsl 发表于 2011-11-14 01:53:21
good luck

报纸
daxia101 发表于 2011-11-14 02:09:35
看例子:

> matrix(1:20,ncol<-4)
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20
> ncol
[1] 4
因此,在函数中只能用 “=” 进行赋值。用 "<-" 时行赋值时, "<-" 及前面的符号都被忽略,然后,值就按顺序进行赋值了。因此
matrix(1:20, ncol <- 4) 与 matrix(1:20, 4) 是等同的,也就是 matrix(1:20, nrow = 4)

至于具体为什么这样,我还不太清楚。
已有 1 人评分经验 论坛币 热心指数 收起 理由
ruiqwy + 40 + 100 + 1 热心帮助其他会员

总评分: 经验 + 40  论坛币 + 100  热心指数 + 1   查看全部评分

地板
franknan 发表于 2011-11-14 05:46:18
个人感觉一般还是直接用 =比较好!
已有 1 人评分论坛币 热心指数 收起 理由
ruiqwy + 100 + 1 热心帮助其他会员

总评分: 论坛币 + 100  热心指数 + 1   查看全部评分

7
qoiqpwqr 发表于 2011-11-14 09:51:53
有的时候可以用 -> 进行赋值而 = 不行
比如想复制一个式子的值给变量x
我经常先输入了一堆的式子,然后突然想起忘在前面加变量名x了
这时候用 -> x 就可以赋值了,而不用把光标移到最前面然后输入 x =

比如
sin(2*6.7)/cos(3) -> x
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
UNC + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

8
shenbaiseshatan 在职认证  发表于 2011-11-14 13:01:35
<-和->是一对,可以向左和向右赋值
=是单向的,作用和<-基本相同,但对函数中的变量通常使用=
<<-这个是全局赋值,跟变量的作用域有关,一般不会用到
已有 4 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
gobaw + 5 + 1 + 1 + 1 精彩帖子
augustin4 + 5 + 1 + 1 热心帮助其他会员
proc.com + 1 + 1 + 1 热心帮助其他会员
UNC + 1 + 1 + 1 热心帮助其他会员

总评分: 论坛币 + 10  学术水平 + 4  热心指数 + 4  信用等级 + 3   查看全部评分

胜人者有力,自胜者强!

9
kk22boy 发表于 2011-11-15 09:14:59
daxia101 发表于 2011-11-14 01:50
What’s the difference?

The main difference between the two assignment operators is scope. It’s  ...
很清楚,谢谢
如果该贴对您有些许帮助,希望你能回复一下或者评一下热心指数!谢谢!

10
Weitaiyun 发表于 2011-11-15 12:19:30
= 是之前参数传递,现在也可以赋值可,但和 <- 还是不对等的,不过差别比较细微。system.time(x <- runif(1000)) 中 <- 就不能换成=
已有 1 人评分论坛币 学术水平 热心指数 收起 理由
augustin4 + 5 + 1 + 1 是这样的

总评分: 论坛币 + 5  学术水平 + 1  热心指数 + 1   查看全部评分

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

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