[R] Simple loop question

Jim Lemon jim at bitwrit.com.au
Fri Mar 28 07:32:38 CET 2014


On 03/28/2014 05:27 PM, Luana Marotta wrote:
> Hi all,
>
> I'm trying to find out what is the equivalent in R for the following Stata
> code:
>
> Let's say I have the variables: a1, a2, a3, a4, a5
>
> forvalues i = 1(1)5 {
>      summary a`i'
> }
>
> That is, I want to know how to loop through variables in R.
>
Hi Luana,
Try this:

for(var in paste("a",1:5,sep="")) print(summary(get(var)))

Jim




More information about the R-help mailing list