[R] function

Marc Schwartz marc_schwartz at comcast.net
Thu Nov 9 19:15:32 CET 2006


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

Bill, using return() in this fashion was deprecated in R version 1.8.0,
which was released 3 years ago.

>From the ONEWS file:

o	The use of multi-argument return() calls is deprecated: use a
	(named) list instead.


and from ?return:


Warning:

     Prior to R 1.8.0, 'value' could be a series of non-empty
     expressions separated by commas.  In that case the value returned
     is a list of the evaluated expressions, with names set to the
     expressions where these are the names of R objects. That is,
     'a=foo()' names the list component 'a' and gives it value the
     result of evaluating 'foo()'.

     This has been deprecated (and a warning is given), as it was never
     documented in S, and whether or not the list is named differs by S
     versions.



Thus:

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


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

$mean
[1] 3

$sd
[1] 1.581139


HTH,

Marc Schwartz



More information about the R-help mailing list