test$x doesn't evaluate the function, you want something like test(1,2)$x, e.g.:
test <- function(i, j){
x <- i:j
y <- i*j
z <- i/j
return(list(x=x,y=y,z=z))
}
test(1,2)$x
[1] 1 2
test(1,2)$y
[1] 2
test(1,2)$z
[1] 0.5
Or if you want to avoid evaluating your function multiple times:
res <- test(1,2)
res$x
[1] 1 2
res$y
[1] 2
res$z
[1] 0.5
res
$x
[1] 1 2
$y
[1] 2
$z
[1] 0.5
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
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
______________________________________________
[email protected] 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.