【发布时间】:2012-11-19 17:04:17
【问题描述】:
我正在使用函数 system.time() 并且我发现了一些令我惊讶的东西。我经常使用分配符号“=”而不是“
代码如下:
a = matrix(1, nrow = 10000)
require(stats)
system.time(a[,1] = a[,1]*2) #this line doesn't work
#Error: unexpected '=' in "system.time(a[,1] ="
system.time(a[,1] = a[,1]*2) #this line works
system.time(for(i in 1:100){a[,1] = a[,1]*i}) #this line works!!!!
我发现:Is there a technical difference between "=" and "<-" 解释了我不能在函数中使用“=”进行分配,因为它是在函数中分配参数的符号。但我惊讶地发现它有时可以工作(见下面的代码)。
有谁知道它为什么在这里工作? (也是为什么它在第一种情况下不起作用,因为我猜 a[,1] 不是函数 system.time()...的参数...)
非常感谢。 埃德温。
【问题讨论】:
-
这已经被问过了。您的代码是有效的,因为它包含在
{ ... }
标签: r assignment-operator