[R] function

Marc Schwartz marc_schwartz at comcast.net
Thu Nov 9 19:28:34 CET 2006


On Thu, 2006-11-09 at 12:15 -0600, Marc Schwartz wrote:
> On Thu, 2006-11-09 at 12:58 -0500, Bill Hunsicker wrote:
> > R-help,
> >  
> > I am trying to create a function that i pass a data set to and have the
> > function return some calculations based on data.
> >  
> > Allow me to illustrate:
> >  
> > myfunc <- function(lst,mn,sd){
> >    lst <- sort(lst)
> >    mn <- mean(lst)
> >    sd <- sqrt(var(lst))
> >    return(lst,mn,sd)
> > }
> >  
> > data1 <-c (1,2,3,4,5)
> > data2 <- c(6,7,8,9,10)
> > myfunc(data1,data1mn,data1sd)
> > myfunc(data2,data2mn,data2sd)
> >  
> > This snippet errors that data1mn not find and warns that return is
> > deprecating!!!!!!!!!!!
> >  
> > Can you help me?
> >  
> > Regards,
> > Bill

> Thus:

> myfunc <- function(lst,mn,sd){
>    lst <- sort(lst)
>    mn <- mean(lst)
>    sd <- sqrt(var(lst))
>    list(lst = lst, mean = mn, sd = sd)
> }


Further tweak, no need to pass the 'mn' and 'sd' argument. Missed that
the first time around:


myfunc <- function(lst){
    lst <- sort(lst)
    mn <- mean(lst)
    sd <- sqrt(var(lst))
    list(lst = lst, mean = mn, sd = sd)
 }
 

data1 <- 5:1

> data1
[1] 5 4 3 2 1

> myfunc(data1)
$lst
[1] 1 2 3 4 5

$mean
[1] 3

$sd
[1] 1.581139

HTH,

Marc



More information about the R-help mailing list