[R] Re use objects from within a custom made function

David Winsemius dwinsemius at comcast.net
Tue Oct 13 04:01:53 CEST 2009


On Oct 12, 2009, at 7:06 PM, Stropharia wrote:

>
> Hi everyone,
>
> i'm having a problem extracting objects out of functions i've  
> created, so i
> can use them for further analysis. Here's a small example:
>
> # ---------------------------
> test <- function(i, j){
> 	x <- i:j
> 	y <- i*j
> 	z <- i/j
> 	return(x,y,z)
> }
> # ---------------------------
>
> This returns the 3 objects as $x, $y and $z. I cannot, however,  
> access these
> objects individually by typing for example:
>
> test$x


Generally one assigns the resutlts of a function to an object name:

 > ll <- test(1,2)
Warning message:
In return(x, y, z) : multi-argument returns are deprecated
 > ll
$x
[1] 1 2

$y
[1] 2

$z
[1] 0.5

 > str(ll)
List of 3
  $ x: int [1:2] 1 2
  $ y: num 2
  $ z: num 0.5


So you would not get the warnings if you instead ended the function  
with:

  return(list(x,y,z) )

>
> I know i can do this by adding an extra arrow head to the assignment  
> arrow
> (<<-), but I am sure that is not how it is done in some of the  
> established R
> functions (like when calling lm$coef out of the lm function). Is  
> there a
> simple command i've omitted from the function that allows access to  
> objects
> inside it?
>
> Thanks in advance,
>
> Steve
> -- 

--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list