Re: [R] system.time gives error when "=" is used for assignment(R-2.6.0)

From: <Bill.Venables_at_csiro.au>
Date: Fri, 11 Apr 2008 12:10:53 +1000

 

Kyeongmi asks:

> Hello list,
>
> I found that system.time works correctly when I used "<-" to assign
> a value to a variable but when I happened to use "=" instead of
> "<-", R gave an error message: "Error in system.time(your argument
> here...)". It happened with a few functions I tried. Is this a bug
> or is there any circumstances that "=" cannot be used for
> assignment? Here is a real simple example.
>
> fn1 <- function(x) x+1
>
> r1 <- system.time(res1=fn1(2))
> r2 <- system.time(res2 <- fn1(2))
>

No, it is not a bug. It is a syntactic trap that using '=' for assignment will lure you into. This is one reason why I suggest you just do not do it.

Think about it. If the function system.time() had an argument res1, how would you call it giving a value for this argument? As you have done. So it has nothing to do with the function system.time iteslf, it applies generally.

Ways around it include

r1 <- system.time((res1 = fn1(2)))  ## if you must!
r1 <- system.time({res1 = fn1(2)})  ## ditto
r1 <- system.time(res1 <- fn1(2))   ## as you discovered.

Bill Venables.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 11 Apr 2008 - 02:13:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Apr 2008 - 02:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive