[R] help with a for loop

jim holtman jholtman at gmail.com
Thu Oct 16 01:53:43 CEST 2008


I am not sure what you mean by "p is not updated".  I took the basic
outline you have and as you can see, p has the correct value after the
loop.

> p<-0.1
> n<-10
> result<-list()
> while (p<=1) {
+     for (i in 1:n) {
+         result[[i]]<-p
+         p<-p+0.1
+         print(p)
+     }
+ }
[1] 0.2
[1] 0.3
[1] 0.4
[1] 0.5
[1] 0.6
[1] 0.7
[1] 0.8
[1] 0.9
[1] 1
[1] 1.1
> p
[1] 1.1
>
>


On Wed, Oct 15, 2008 at 3:56 PM, Quan Li <quanli at mail.ucf.edu> wrote:
> Hi all,
>
> I am running OLS models with a power parameter: y=a+bx^p, and I want to iterate through p with a set increment. The function I constructed is something like below:
>
> test<-function(data, model,...){
> f<-formula(model) #model="y~I(x^p)"
> p<-0.1
> n<-10
> result<-list()
> while (p<=1) {
> for (i in 1:n) {
> result[[i]]<-lm(data, formula=f...)
> p<-p+0.1
> }
> }
> return(result)
> }
>
> The problem I am facing is that the loop does not assign or update p values. I also tried a nested for loop starting with "for (p in seq(0.1,1,0.1)){for (i in 1:n) {..." but it did not work either. After the initial value of p is assigned, the inner loop is executed but p is not updated. Any help is greatly appreciated.
>
> best,
>
> quan
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list