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 Hunsicker RF Micro Devices 7625 Thorndike Road Greensboro, NC 27409-9421 bhunsicker@rfmd.com 336-678-5260(w) 610-597-9985(m) 336-678-5088(lab) [[alternative HTML version deleted]]
On 11/9/06, Bill Hunsicker <BHunsicker at rfmd.com> wrote:> 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?It is the returning a vector of arguments without wrapping them as a list that is deprecated. You do not need to pass values to be returned as arguments to the function. The simplest way of modifying your function is myfunc <- function(dat) { mn <- mean(dat) stddev <- sd(dat) list(data = dat, mean = mn, sd = stddev) } You could even make it into a one-liner as myfunc <- function(dat) list(data = dat, mean = mean(dat), sd = sd(dat)) but it would be better R programming style to do some checking on the argument 'dat' before trying to apply the mean and sd functions to it.
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, > BillBill, 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
Bill - The error is because data1mn is not defined as anything in your environment, nor are data1sd, data2mn, data2sd. It appears as though you don't want to define these variables to pass into the function as arguments, but rather you want to compute them in your function. In other words, your function does not depend on the values of these are before calling the function, you'd like to computer their values in your function. Your function at this point only depends on the data set, called lst in your case, so you only need to pass that in. The return() call is a different issue. See ?return for an explanation of why you are receiving the warning message. You would like to return the result of more than one computation from your function. You can use a list instead. The version below does not return the sorted dataset as your function does, but only the mean and sd of the data set passed in. You can modify the list() call to return sorted lst if you'd like. I've given the list components names. myfunc <- function(lst) { lst <- sort(lst) #not needed to compute mean or sd, use if you want mn <- mean(lst) sd <- sqrt(var(lst)) #or use sd() in stats package, see ?sd list(mn=mn,sd=sd) #R returns the last expression w/out return() } > myfunc(c(1,2,3,4)) $mn [1] 2.5 $sd [1] 1.290994 > a <- myfunc(c(1,2,3,4)) > a$mn [1] 2.5 > a$sd [1] 1.290994 > a $mn [1] 2.5 $sd [1] 1.290994 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 Hunsicker > RF Micro Devices > 7625 Thorndike Road > Greensboro, NC 27409-9421 > bhunsicker at rfmd.com > 336-678-5260(w) > 610-597-9985(m) > 336-678-5088(lab) > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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.