[R] print and rbind within a for loop
Steven LeBlanc
oreslag at gmail.com
Wed Mar 25 21:04:15 CET 2015
Hi Dave,
Thank you, but that didn't seem to do it. This code produces the same result:
sim.app.wald<-function(B=0.1,min=10,max=50,alpha=0.05){
result<-c(fails=0,n=0)
for(n in min:max){
x<-seq(1,n-1,1)
fhat<-x/n
se<-sqrt((fhat*(1-fhat))/n)
ci.wald<-fhat+qnorm(1-(alpha/2))*se
if(sum(ci.wald[ci.wald<=B])){
result<-rbind(cbind(fails=x[ci.wald<=B],n=n),result)
print(cbind(X=x[ci.wald<=B],n))
}
return(result)
}
}
Once again, commenting out the return() statement shows the correct output via the print() statement.
Best Regards,
Steven
On Mar 25, 2015, at 12:51 PM, David R Forrest <drf at vims.edu> wrote:
> From looking at it it looks like nothing is done with the results of rbind(), and the return() then exits the loop and routine with the unmodified result.
>
> Dave
> ________________________________________
> From: R-help [r-help-bounces at r-project.org] on behalf of Steven LeBlanc [oreslag at gmail.com]
> Sent: Wednesday, March 25, 2015 3:43 PM
> To: r-help at R-project.org
> Subject: [R] print and rbind within a for loop
>
> Greets,
>
> I'm trying to iteratively find solutions to a problem given a range of options. Code is as follows:
>
> sim.app.wald<-function(B=0.1,min=10,max=50,alpha=0.05){
> result<-c(fails=0,n=0)
> for(n in min:max){
> x<-seq(1,n-1,1)
> fhat<-x/n
> se<-sqrt((fhat*(1-fhat))/n)
> ci.wald<-fhat+qnorm(1-(alpha/2))*se
> if(sum(ci.wald[ci.wald<=B])){
> # rbind(cbind(fails=x[ci.wald<=B],n=n),result)
> print(cbind(X=x[ci.wald<=B],n))
> }
> # return(result)
> }
> }
>
> If you run it like this, you can readily see what I intend to get out; which is the correct result. If you uncomment the two commented lines, nothing useful obtains.
>
> What am I missing here? This seeming codependence of print() and return() and the failure of rbind() to do anything is quite puzzling.
>
> Best Regards,
> Steven
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]
More information about the R-help
mailing list