Hi Michael, Thanks for your answer! sorry I didn't see this rule... No problem, I'll try to figure it out by myself.
Thanks for your indication though! Thomas 2012/1/4, Michael Weylandt [via R] <ml-node+s789695n4261894...@n4.nabble.com>: > > > Unfortunately, there's a general no-homework rule because we never > know what your instructor wants you to figure out on your own (though, > a sincere thanks for admitting this was hw rather than trying to trick > us like so many). That said, I think your second function, while a > little clumsy, does work but for one little error near the end. It's > very obvious when you see it so don't overthink it, but look closely > at the lines that involve "swap". > > If you want to make it more elegant, I'd suggest you look at the > ?switch construct: also, to make the swap bit more elegant, think > about how one exchanges variable values in a language like C and put > some construct like that within an "if" statement near the top. If you > want to get real elegant, note that after you figure out switch, > constructs like `+`(3, 4) are valid in R (though certainly beyond the > scope of your exercise) > > Hope this helps, > > Michael > > On Wed, Jan 4, 2012 at 9:45 AM, Thomthom <rime.tho...@gmail.com> wrote: >> Hi R helpers! >> >> I have a question. I'm trying to create a function for an exercise. Here >> are >> the arguments I should include: >> >> x and y are numeric >> z is a name ("plus","minus","multiply","divide") >> and swap is logical. >> >> Here is what the function should do: >> >> When z="plus", then x+y is performed and so on for the other z names. It >> should give a NA when the z argument doesn't correspond to something >> mention >> before. >> When swap =TRUE, then y must be on the left side of the equation (y/x >> instead of x/y). >> >> So far, I managed to get something worked for the first part of the body >> function (typing "plus" performes indeed an addition...) but I couldn't >> get >> how to make the second part working. Any suggestion? >> >> Here is the code I have so far: >> >> myFunc<- function(x,y,z,swap) >> Â Â Â Â Â Â Â Â { res= NULL >> Â Â Â Â Â Â Â Â Â if(z =="plus") res=x+y >> Â Â Â Â Â Â Â Â Â else if(z=="minus") res=x-y >> Â Â Â Â Â Â Â Â Â else if(z== "multiply") res=x*y >> Â Â Â Â Â Â Â Â Â else if(z=="divide") res=x/y >> Â Â Â Â Â Â Â Â Â else res<-NA >> >> Â Â Â Â Â Â Â Â Â return(res)} >> >> Then, I tried to include some lines to take this "swap" function into >> account by "splitting" somehow my function in 2: >> >> myFunc<- function(x,y,z,swap) >> Â Â Â Â Â Â Â Â { res1= NULL >> Â Â Â Â Â Â Â Â Â res2=NULL >> >> Â Â Â Â Â Â Â Â Â if(z =="plus") res1=x+y >> Â Â Â Â Â Â Â Â Â else if(z=="minus") res1=x-y >> Â Â Â Â Â Â Â Â Â else if(z== "multiply") res1=x*y >> Â Â Â Â Â Â Â Â Â else if(z=="divide") res1=x/y >> Â Â Â Â Â Â Â Â Â else res1<-NA >> >> Â Â Â Â Â Â Â Â Â if(z =="plus") res2=y+x >> Â Â Â Â Â Â Â Â Â else if(z=="minus") res2=y-x >> Â Â Â Â Â Â Â Â Â else if(z== "multiply") res2=y*x >> Â Â Â Â Â Â Â Â Â else if(z=="divide") res2=y/x >> Â Â Â Â Â Â Â Â Â else res2<-NA >> >> Â Â Â Â Â Â Â Â Â if (swap<-T) return(res2) >> Â Â Â Â Â Â Â Â Â else return(res1) >> Â Â Â Â Â Â Â } >> Any suggestions would be more than appreciated! I already thank you! >> >> And happy new year! (that may be redundant to most of you I know ^^) >> >> Thomas >> >> -- >> View this message in context: >> http://r.789695.n4.nabble.com/function-in-R-for-my-exercise-tp4261710p4261710.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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. > > > _______________________________________________ > If you reply to this email, your message will be added to the discussion > below: > http://r.789695.n4.nabble.com/function-in-R-for-my-exercise-tp4261710p4261894.html > > To unsubscribe from function in R for my exercise, visit > http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4261710&code=cmltZS50aG9tYXNAZ21haWwuY29tfDQyNjE3MTB8LTg1ODkwMDI4MA== -- Thomas -- View this message in context: http://r.789695.n4.nabble.com/function-in-R-for-my-exercise-tp4261710p4262273.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
______________________________________________ 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.