On Dec 2, 2010, at 10:47 PM, Erik Iverson wrote:
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. :)
Certain functions (so-called "primitive" functions) are entirely
driven by positional matching, although matrix is not one of those
functions. Erik is telling you that your use of ncol<-4 got evaluated
to 4 and that the name of the resulting object was ignored, howevert
the value of the operation was passed on to matrix which used
positional matching since "=" was not used. Usually the problem facing
newbies is that they want to save keystrokes and so use "=" for
assignment (also a potential pitfall although not as likely to mess
you up as the choice to use the two-keystroke path for argument
assignment).
"<-" is really a function and returns a value in addition to having
a side-effect of assigning a value to an object.
> y <- eval(x<-4)
> y
[1] 4
--Erik
Best regards,
Jinsong
______________________________________________
R-help@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.
______________________________________________
R-help@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.
David Winsemius, MD
West Hartford, CT
______________________________________________
R-help@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.