On Sat, Jul 18, 2009 at 5:47 PM, Bernd<prof7...@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? >
It's a sprinkle of syntactic sugar: colnames(x)=c("a","b") is the same as: x = "colnames<-"(x,c("a","b")) So there is a function called "colnames<-". See it by typing: get("colnames<-") and you'll see it has args x and value, and returns the modified x. You can write your own foo<- functions and they operate like that. This is probably documented somewhere... Barry ______________________________________________ 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.