[R] saving object function

Joshua Wiley jwiley.psych at gmail.com
Fri Oct 8 08:42:56 CEST 2010


Hi Joe,

You can just put the results into a named list, for example:

functest<-function(x){
  a <- x + 1
  b <- x + 2
  c <- x + 3
  results <- list("a" = a, "b" = b, "c" = c)
  return(results)
}

functest(1)$a

It is important to name the list or you would have to refer to it as:
functest(1)[[1]]  for the first element and so on.

HTH,

Josh

On Thu, Oct 7, 2010 at 11:30 PM, Joe P King <jp at joepking.com> wrote:
> Ok so if I have a function:
>
>
>
> functest<-function(x){
>
>            a<-x+1
>
>            b<-x+2
>
>            c<-x+3
>
> paste(a)
>
> paste(b)
>
> paste(c)
>
> }
>
>
>
> Now I know I can do cat(), or return() on one of them but if I was to run
> the function with any number, how could I create objects to save so I could
> do, I am wondering if I have more than one object within my function.
>
>
>
> functest$a and get the result. I hope this is clear.
>
>
>
> -------------------------------------------
>
> Joe King, M.A.
>
> Ph.D. Student
>
> University of Washington - Seattle
>
> 206-913-2912
>
>  <mailto:jp at joepking.com> jp at joepking.com
>
> -------------------------------------------
>
> "Never throughout history has a man who lived a life of ease left a name
> worth remembering." --Theodore Roosevelt
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list