Hi just a quick question in which I must use the "function and return" part of R. If I was asked to define a function called "median" where the function has a single argument x and returns the median value of a vector. How would I go about saying the median = element x in terms of n (say (n + 1)/2 or whatever it would be) if there is an even amount of elements, or x in terms of n (with a different way of saying how many n) if there is an odd amount of elements? Much appreciated elliot.welch at virgin.net Sent from my BlackBerry? smartphone
I'd start by using if(). If you can convince the list that this isn't homework, you might get more help. Sarah On Mon, Mar 12, 2012 at 7:15 AM, <elliot.welch at virgin.net> wrote:> Hi just a quick question in which I must use the "function and return" part of R. > > If I was asked to define a function called "median" where the function has a single argument x and returns the median value of a vector. How would I go about saying the median = element x in terms of n (say (n + 1)/2 or whatever it would be) if there is an even amount of elements, or x in terms of n (with a different way of saying how many n) if there is an odd amount of elements? > > Much appreciated >-- Sarah Goslee http://www.functionaldiversity.org
Hi, On Mon, Mar 12, 2012 at 12:10 PM, <elliot.welch at virgin.net> wrote:> I've tried the if function? Its just defining when something is even or odd?R's help system might be useful. www.rseek.org might be useful. You haven't accomplished the second part of my response, and this isn't a homework help list, so you'll need to do your own work from here. Sarah> ------Original Message------ > From: Sarah Goslee > To: elliot.welch at virgin.net > Cc: R-help at stat.math.ethz.ch > Subject: Re: [R] Finding the median > Sent: Mar 12, 2012 15:59 > > I'd start by using if(). > > If you can convince the list that this isn't homework, you might get more help. > > Sarah > > On Mon, Mar 12, 2012 at 7:15 AM, ?<elliot.welch at virgin.net> wrote: >> Hi just a quick question in which I must use the "function and return" part of R. >> >> If I was asked to define a function called "median" where the function has a single argument x and returns the median value of a vector. How would I go about saying the median = element x in terms of n (say (n + 1)/2 or whatever it would be) if there is an even amount of elements, or x in terms of n (with a different way of saying how many n) if there is an odd amount of elements? >> >> Much appreciated >>-- Sarah Goslee http://www.functionaldiversity.org
Did you try what I suggested? If you go to www.rseek.org and search for "odd or even integers" you will get various solutions. You could even write your own test by comparing x/2 with round(x/2). Also, it's good form to copy the list on your replies, not just me. Sarah On Mon, Mar 12, 2012 at 12:29 PM, <elliot.welch at virgin.net> wrote:> I can't find it that thing. And its not a homework task, I work for a company who require this function to do analysis on some data but we're all new to R? > ------Original Message------ > From: Sarah Goslee > To: elliot.welch at virgin.net > To: r-help > Subject: Re: [R] Finding the median > Sent: Mar 12, 2012 16:14 > > Hi, > > On Mon, Mar 12, 2012 at 12:10 PM, ?<elliot.welch at virgin.net> wrote: >> I've tried the if function? Its just defining when something is even or odd? > > R's help system might be useful. www.rseek.org might be useful. > > You haven't accomplished the second part of my response, and this > isn't a homework help list, so you'll need to do your own work from > here. > > Sarah > >> ------Original Message------ >> From: Sarah Goslee >> To: elliot.welch at virgin.net >> Cc: R-help at stat.math.ethz.ch >> Subject: Re: [R] Finding the median >> Sent: Mar 12, 2012 15:59 >> >> I'd start by using if(). >> >> If you can convince the list that this isn't homework, you might get more help. >> >> Sarah >> >> On Mon, Mar 12, 2012 at 7:15 AM, ?<elliot.welch at virgin.net> wrote: >>> Hi just a quick question in which I must use the "function and return" part of R. >>> >>> If I was asked to define a function called "median" where the function has a single argument x and returns the median value of a vector. How would I go about saying the median = element x in terms of n (say (n + 1)/2 or whatever it would be) if there is an even amount of elements, or x in terms of n (with a different way of saying how many n) if there is an odd amount of elements? >>> >>> Much appreciated >>>-- Sarah Goslee http://www.functionaldiversity.org