An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111011/04b86ff5/attachment.pl>
stop()
6 messages · Dimitris Rizopoulos, Doran, Harold, Nordlund, Dan (DSHS/RDA) +1 more
You could use return(), e.g.,
myFun <- function (x, max.iter = 5) {
for (i in 1:10) {
result <- x + i
iter <- i
if (iter == max.iter) {
return(result)
}
}
result
}
myFun(10, max.iter = 4)
I hope it helps.
Best,
Dimitris
On 10/11/2011 7:31 PM, Doran, Harold wrote:
Suppose I have a function, such as the toy example below:
myFun<- function(x, max.iter = 5) {
for(i in 1:10){
result<- x + i
iter<- i
if(iter == max.iter) stop('Max reached')
}
result
}
I can of course do this:
myFun(10, max.iter = 11)
However, if I reach the maximum number of iterations before my "algorithm" has finished (in my real application there are EM steps for a mixed model), I actually want the function to return the value of "result" up to that point. Currently using stop(), I would get
myFun(10, max.iter = 4)
Error in myFun(10, max.iter = 4) : Max reached But, in this toy case the function should return the value of "result" up to iteration 4. Not sure how I can adjust this. Thanks, Harold [[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.
Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
Thanks, Dimitris. Very helpful on something I *should* know by now.
-----Original Message-----
From: Dimitris Rizopoulos [mailto:d.rizopoulos at erasmusmc.nl]
Sent: Tuesday, October 11, 2011 1:43 PM
To: Doran, Harold
Cc: r-help at r-project.org
Subject: Re: [R] stop()
You could use return(), e.g.,
myFun <- function (x, max.iter = 5) {
for (i in 1:10) {
result <- x + i
iter <- i
if (iter == max.iter) {
return(result)
}
}
result
}
myFun(10, max.iter = 4)
I hope it helps.
Best,
Dimitris
On 10/11/2011 7:31 PM, Doran, Harold wrote:
Suppose I have a function, such as the toy example below:
myFun<- function(x, max.iter = 5) {
for(i in 1:10){
result<- x + i
iter<- i
if(iter == max.iter) stop('Max reached')
}
result
}
I can of course do this:
myFun(10, max.iter = 11)
However, if I reach the maximum number of iterations before my "algorithm"
has finished (in my real application there are EM steps for a mixed model), I actually want the function to return the value of "result" up to that point. Currently using stop(), I would get
myFun(10, max.iter = 4)
Error in myFun(10, max.iter = 4) : Max reached But, in this toy case the function should return the value of "result" up to
iteration 4.
Not sure how I can adjust this. Thanks, Harold [[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.
-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Dimitris Rizopoulos
Sent: Tuesday, October 11, 2011 10:43 AM
To: Doran, Harold
Cc: r-help at r-project.org
Subject: Re: [R] stop()
You could use return(), e.g.,
myFun <- function (x, max.iter = 5) {
for (i in 1:10) {
result <- x + i
iter <- i
if (iter == max.iter) {
return(result)
}
}
result
}
myFun(10, max.iter = 4)
I hope it helps.
Best,
Dimitris
Or, just use break :
myFun <- function (x, max.iter = 5) {
for (i in 1:10) {
result <- x + i
iter <- i
if (iter == max.iter) break
}
result
}
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
On 10/11/2011 7:31 PM, Doran, Harold wrote:
Suppose I have a function, such as the toy example below:
myFun<- function(x, max.iter = 5) {
for(i in 1:10){
result<- x + i
iter<- i
if(iter == max.iter) stop('Max reached')
}
result
}
I can of course do this:
myFun(10, max.iter = 11)
However, if I reach the maximum number of iterations before my
"algorithm" has finished (in my real application there are EM steps for a mixed model), I actually want the function to return the value of "result" up to that point. Currently using stop(), I would get
myFun(10, max.iter = 4)
Error in myFun(10, max.iter = 4) : Max reached But, in this toy case the function should return the value of
"result" up to iteration 4.
Not sure how I can adjust this. Thanks, Harold [[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.
-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
______________________________________________ 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.
Replace "stop()" with "break" to see if that does what you want. (you may also want to include "cat()" or "warn()" to indicate the early stopping.
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Doran, Harold
> Sent: Tuesday, October 11, 2011 11:32 AM
> To: r-help at r-project.org
> Subject: [R] stop()
>
> Suppose I have a function, such as the toy example below:
>
> myFun <- function(x, max.iter = 5) {
> for(i in 1:10){
> result <- x + i
> iter <- i
> if(iter == max.iter) stop('Max reached')
> }
> result
> }
>
> I can of course do this:
> myFun(10, max.iter = 11)
>
> However, if I reach the maximum number of iterations before my
> "algorithm" has finished (in my real application there are EM steps for
> a mixed model), I actually want the function to return the value of
> "result" up to that point. Currently using stop(), I would get
>
> > myFun(10, max.iter = 4)
> Error in myFun(10, max.iter = 4) : Max reached
>
> But, in this toy case the function should return the value of "result"
> up to iteration 4.
>
> Not sure how I can adjust this.
>
> Thanks,
> Harold
>
>
>
> [[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.
Thank you, Greg. This indeed works well for this purpose.
-----Original Message----- From: Greg Snow [mailto:Greg.Snow at imail.org] Sent: Tuesday, October 11, 2011 4:27 PM To: Doran, Harold; r-help at r-project.org Subject: RE: stop() Replace "stop()" with "break" to see if that does what you want. (you may also want to include "cat()" or "warn()" to indicate the early stopping. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Doran, Harold
Sent: Tuesday, October 11, 2011 11:32 AM
To: r-help at r-project.org
Subject: [R] stop()
Suppose I have a function, such as the toy example below:
myFun <- function(x, max.iter = 5) {
for(i in 1:10){
result <- x + i
iter <- i
if(iter == max.iter) stop('Max reached')
}
result
}
I can of course do this:
myFun(10, max.iter = 11)
However, if I reach the maximum number of iterations before my
"algorithm" has finished (in my real application there are EM steps for
a mixed model), I actually want the function to return the value of
"result" up to that point. Currently using stop(), I would get
myFun(10, max.iter = 4)
Error in myFun(10, max.iter = 4) : Max reached But, in this toy case the function should return the value of "result" up to iteration 4. Not sure how I can adjust this. Thanks, Harold [[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.