Re: [R] basic information defining functions

From: Bunny, <>
Date: Tue, 25 Nov 2008 18:52:40 +0100

Thank very much ! Indexing is exactly the problem. I think itīs not because somebody wrote a bad doc, itīs rather because being an amateur PHP etc scripter i did not realize the power of this indexing yet. Sometime ago i surely would have used a lot more loops instead of proper indexing.

  Basically i guess this is due to the fact that i was reluctant to search for ?$. I did simply not consider that this was possible. Itīs always difficult to google respectively search for for operators in general.
I believe most documentations i read were some minor documentations made by universities for some basic statistics. Some popular document i really like is Farnsworth Econometrics in R, but searching for shortcut it does not offer the result i needed. Indeed, if i had looked for [[ i would have found something ...

Imho, the indexing is something very special in R, perhaps it would help newbies if documentations point it out more precisely in its table of content, using some terminology that might help to find the part about indexing. e.g. : indexing operators, shortcuts vs. indexing , or dynamic or computed indexing as you said.

Am 25.11.2008 um 17:13 schrieb Greg Snow:

> I think your problem is more with indexing than with function
> writing. The main confusion is in how to use '$', this is a
> shortcut to make certain things easier, but you are trying to use
> the shortcut like going from France to Germany by way of New York
> City because you know a great shortcut through NYC.
> From the help page for $ (> ?'$'):
> "Both '[[' and '$' select a single element of the list. The main
> difference is that '$' does not allow computed indices, whereas
> '[[' does."
> You are trying to use a computed index, look at the line above
> (especially the 'not') and see if there is something else there
> suggests what you should be using.
> This seems to be a common misunderstanding lately, could you tell us
> a bit about which documentation and examples you read that lead you
> to think about using '$' with computed indices and did not help you
> understand the need for '[[' instead so that maybe the documentation
> can be improved to help future readers?
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> 801.408.8111
>> -----Original Message-----
>> From: [mailto:r-help-bounces_at_r-
>>] On Behalf Of Bunny,
>> Sent: Tuesday, November 25, 2008 3:18 AM
>> To:
>> Subject: [R] basic information defining functions
>> Hi all,
>> i am looking from some insights to define own R functions. so far i
>> found most basics in documentations that are around on the web.
>> except
>> for one thing:
>> Iīd like to define some function, say:
>> #assume my data matrix contains vectors like data$myColumn1,data
>> $myColumn2 etc.
>> getMyColumn <- function (columnid){
>> x<-data$MyColumn?columnid?[data$indexone=1 & data$index2=5]
>> return(x)
>> }
>> Do I need to use assign or eval first ? I tried to use paste to
>> combine something like: paste("data$MyColumn",columnid,sep="") which
>> did not work.
>> I am happy to get any help with the problem, but also thankful for
>> some useful link or guide on how to define own functions properly,
>> especially the dynamic naming and return part
>> thx in advance
>> bunny
>> ______________________________________________
>> mailing list
>> PLEASE do read the posting guide
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 25 Nov 2008 - 17:54:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 25 Nov 2008 - 18:30:28 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive