[R] loop function

Adams, Jean jvadams at usgs.gov
Thu Aug 18 20:49:03 CEST 2016


You seemed to have re-written over the "b" object in your code.
This might work for you.

library(MASS)
for (i in 58:1){
  for(j in 58:i){
    str.temp <- paste("y1 ~ x", i, "* x", j, sep = "")
    univar <- glm.nb(as.formula(str.temp), data=df)
    b <- summary(univar)$coeffients[4, 4]
    if(b < 0.6){
      cat(i, j, b, "\n")
    }
  }
}

By the way, you should post using plain text (not html).

Jean


On Thu, Aug 18, 2016 at 6:48 AM, fatimah soleimany <
fatimah.soleimany at gmail.com> wrote:

> Hi Dear users,
> for an interactive use, i am trying to write a loop that looks for all
> variables in the conditions which i introduced, this is what I'm trying:
>
> library(MASS)> i=1> for (i in 58:1){+ for(j in 58:i){+ + str.temp <-
> paste("y1 ~ x", i, "* x", j, sep = "")+
> univar<-glm.nb(as.formula(str.temp), data=df)+
> b=summary(univar)$coeffients[4,4]+ b<-c(i,j)+ if(b < 0.6){ +
> print(b)+    }+   }+   }
>
> i have no error but i didn't get true response too, my result is this:
>
>
> b[1] 1 1
>
>  but i want all the variables that their p-value are less than 0.6, i
> think i couldn't write a true loop,can you help me?
>
> thank you in advance for any help
>
>         [[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