[R] assignments to function return values

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jul 18 19:11:28 CEST 2009


To get help on a "replacement function" try this:

?"colnames<-"

and to see its source:

`colnames<-`


On Sat, Jul 18, 2009 at 12:47 PM, Bernd<prof7bit at googlemail.com> wrote:
> colnames(m) = c("a", "b")
>
> I am fairly new to R and trying to understand this language. Having
> learned quite a few other programming languages the above statement
> when i saw it first immediately led to two reactions:
>
> (1) wtf?
>
> (2) maybe they made the function return an object with overloaded
> assignment operator, lets try this:
>
> x = colnames(m)
> x = c("c","d")
>
> but this doesnt work this way and obviously there must be some other
> mechanisms at work here. I opened the r-intro document
> http://cran.r-project.org/doc/manuals/R-intro.html and read through
> chapter 10 (Writing your own functions) but i didnt see one single
> mention of this strange syntax, yet it is used in some example code
> here (10.6.2) but the reader is left with a big question mark in his
> mind and no single word mentions this outstanding unusualness while
> all other aspects of functions that are pretty common with most other
> languages in existance are dealt with in great depth.
>
> I then searched for some hints in the description of the assignment
> operator and the asign() function but didnt find anything there also.
>
> In my oppinion the R documentation could be greatly improved by giving
> each chapter a list of links to all other chapters of the
> documentation needed to understand the example code of this chapter
> (all things that are not immediately obvious to someone coming from a
> more mainstream language) and thus making each chapter a self
> contained possible entry point into the complete documentation. This
> would of course lead to some redundance for the documentation as a
> whole but it would IMHO greatly improve the learning experience.
>
> And here comes my question: How can the above mentioned syntax be
> explained, how can i define such functions with can work in this
> strange inverse manner, where should i have searched to find the
> answer myself? Is there a document like "R for programmers" which
> completely leaves out all the basic stuff and concentrates on the
> obvious differences to most common mainstream languages only?
>
> TIA
> Bernd
>
> ______________________________________________
> 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