[R] using ``<-'' in function argument

Erik Iverson eriki at ccbr.umn.edu
Fri Dec 3 04:47:11 CET 2010


On 12/02/2010 09:35 PM, Jinsong Zhao wrote:
> Hi there,
>
> In function, it's usually using ``='' to assign default value for function
> argument. For newbie, it's possible to using ``<- '' to assign value for
> function argument. Although it's not a correct way, R don't give any warning
> message.
>
>  > 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
>
> It seems that R ignore the ``ncol <-'' and assign the value 4 to ``nrow''.
>
> Would anyone here give a more detailed explanation about this? Thanks in advance!

Yes, R gives the global variable ncol a value of 4. Try typing

 > ncol
before and after your example.

The second argument to the matrix function is now the value 4, and R uses
positional matching to give that argument (nrow) a value of 4. Thus,
the result.

Moral of the story: don't do that. :)
--Erik

>
> Best regards,
> Jinsong
>
> ______________________________________________
> 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.



More information about the R-help mailing list