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

Charles C. Berry cberry at tajo.ucsd.edu
Fri Apr 11 04:09:08 CEST 2008


On Thu, 10 Apr 2008, Kyeongmi Cheon wrote:

> 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.

The latter. Although some might say the former, too. And some will no 
doubt flame me for even saying that. The former, I mean.

Arguments in calls to function (aka arglists) can be in the name=object 
form.

So,

 	(function(x,y) x-y)(y=3,x=2)

evaluates to -1, while

 	(function(x,y) x-y)( 3, 2 )

evaluates to 1, as does

 	 (function(x,y) x-y)(me <- 3, you <- 2)
but

 	 (function(x,y) x-y)(me = 3, you = 2)

gives an error when it tries to match 'me' or 'you' to an argument but 
fails to find one.

Hint: often

 	system.time( { result.of.long.compute <- foo(my.args) } )

is what you want.

HTH,

Chuck

p.s. As an exercise for the reader, why does

  	(function(x,y) x-y)({me = 3},{you = 2})

not return an error?

>
> fn1 <- function(x) x+1
>
> r1 <- system.time(res1=fn1(2))
> r2 <- system.time(res2 <- fn1(2))
>
>
> Thank you.
> Kyeongmi
> University of Memphis
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list