[R] function in R for my exercise

R. Michael Weylandt michael.weylandt at gmail.com
Wed Jan 4 17:30:16 CET 2012


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.thomas at 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 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