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
Richard.Cotton at hsl.gov.uk
2008-Nov-25 11:20 UTC
[R] basic information defining functions
> 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.Do you really mean data matrix, or do you mean data frame?> getMyColumn <- function (columnid){ > > x<-data$MyColumn?columnid?[data$indexone=1 & data$index2=5] > > return(x) > > }It's not terribly clear what you want to do with this function. Question marks don't mean anything in the middle of a statement, so I don't know what data$MyColumn?columnid?[data$indexone=1 & data$index2=5] is supposed to do. Also, you don't need the return keyword - the last line of a function is the return value.> 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.paste returns a string, not a variable. You can evaluate the contents of the string using eval(parse(text=paste("data$MyColumn",columnid,sep=""))) This is however a method of last resort; there is almost always a better way to do things than using eval.> 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 partYou probably don't need to bother with dynamic naming, but take a look at FAW on R, q7.21. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
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 greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Bunny, lautloscrew.com > Sent: Tuesday, November 25, 2008 3:18 AM > To: r-help at r-project.org > 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 > > ______________________________________________ > 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.