Hi, say I got the function:> x=function(nbr){y<-rnorm(nbr);y1 <- mean(y);plot(y)}how can I retrieve value of y1, when I need it. I don't want:> x=function(nbr){y<-rnorm(nbr);y1 <<- mean(y);plot(y)} > y1I want someting like: "x$y1" and then I get the value Many thanks, robert -- View this message in context: http://n4.nabble.com/retrieve-from-function-tp1561972p1561972.html Sent from the R help mailing list archive at Nabble.com.
[This email is either empty or too large to be displayed at this time]
Hi Robert,
You need to modify your function to return a value. Something like
x <- function(nbr){
y<-rnorm(nbr)
y1 <- mean(y)
plot(y)
return(y1)
}
-Ista
Thank you for response. The problem is that using return(y1) in my function formula always returns y1, but what I want is to return it only when I wish, like p.value in t.test(rnorm(100),rnorm(100))$p.value robert -- View this message in context: http://n4.nabble.com/retrieve-from-function-tp1561972p1562012.html Sent from the R help mailing list archive at Nabble.com.
Try this:
nbr <- 30
lapply(body(x), eval)[[grep("y1", body(x))]]
On Fri, Feb 19, 2010 at 3:39 PM, threshold <r.kozarski at gmail.com>
wrote:>
> Hi, say I got the function:
>> x=function(nbr){y<-rnorm(nbr);y1 <- mean(y);plot(y)}
>
> how can I retrieve value of y1, when I need it.
>
> I don't want:
>> x=function(nbr){y<-rnorm(nbr);y1 <<- mean(y);plot(y)}
>> y1
>
> I want someting like:
> "x$y1" and then I get the value
>
> Many thanks, robert
>
>
>
>
>
> --
> View this message in context:
http://n4.nabble.com/retrieve-from-function-tp1561972p1561972.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
--
Henrique Dallazuanna
Curitiba-Paran?-Brasil
25? 25' 40" S 49? 16' 22" O
Try
f <- function(nbr){
y<-rnorm(nbr)
y1 <- mean(y)
plot(y)
invisible( y1)
}
That will return y1 invisibly, so
f(100)
plots but returns nothing visible but
w<-f(100)
plots and places the return value in w
>>> Dennis Murphy <djmuser at gmail.com> 02/19/10 9:33 PM
>>>
Hi:
Perhaps you want this:
f <- function(nbr){
y<-rnorm(nbr)
y1 <- mean(y)
plot(y)
list(y1 = y1)
}
f(100) prints out the mean and executes the plot
w <- f(100) executes the plot> w$y1
[1] 0.06965205
returns the mean as a component of the object w.
HTH,
Dennis
On Fri, Feb 19, 2010 at 10:06 AM, threshold <r.kozarski at gmail.com>
wrote:
>
> Thank you for response. The problem is that using return(y1) in my
function> formula always returns y1, but what I want is to return it only when I
> wish,
> like p.value in
> t.test(rnorm(100),rnorm(100))$p.value
>
> robert
> --
> View this message in context:
> http://n4.nabble.com/retrieve-from-function-tp1561972p1562012.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
[[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.
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
Hi r-help-bounces at r-project.org napsal dne 19.02.2010 19:06:52:> > Thank you for response. The problem is that using return(y1) in myfunction> formula always returns y1, but what I want is to return it only when Iwish,> like p.value in > t.test(rnorm(100),rnorm(100))$p.valuePut results in your function to list. x=function(nbr){list(y<-rnorm(nbr), y1 <- mean(y);plot(y))} x(whatever)$y x(whatever)$y1 and when you do not want results immediately printed consult ?invisible Regards Petr> > robert > -- > View this message in context:http://n4.nabble.com/retrieve-from-function-> tp1561972p1562012.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.