[R] foreach loop, stata equivalent
Milan Bouchet-Valat
nalimilan at club.fr
Mon Feb 18 15:43:57 CET 2013
Le lundi 18 février 2013 à 13:48 +0100, Jamora, Nelissa a écrit :
> Hi! I'm a recent convert from Stata, so forgive my ignorance.
>
>
>
> In Stata, I can write foreach loops (example below)
>
>
>
> foreach var of varlist p1-p14 {
>
> foreach y of varlist p15-p269 {
>
> reg `var' `y'
>
> }
>
> }
>
>
>
> It's looping p1-p15, p1-p16...., p1-p269, p2-p15, p2-p16,... p2-p269,...
> variable pairs.
>
>
>
> How can I write something similar in R?
>
> I 'tried' understanding the package.foreach but can't get it to work.
You do not need package foreach, which is intended at a completely
different problem.
R does not really have the syntactic equivalent of "varlist", but you
can easily do something like:
for(var in paste0("p", 1:14)) {
for(y in paste0("p", 15:269))
lm(yourData[[var]] ~ yourData[[y]])
}
provided that yourData is the data frame in which the p* variables are
stored.
There are probably more direct ways of doing the same thing and storing
the resulting lm objects in a list, but you did not state what you
intend to do with this enormous set of regressions...
Regards
> Thanks for any help
>
> Nelissa
>
>
> [[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.
More information about the R-help
mailing list