[R] how to loop through variables in R?

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Mon Nov 22 10:44:20 CET 2010


watashi at post.com wrote:
> -----Original Message-----
> From: jas4710 <watashi at post.com>
> To: r-help at r-project.org
> Sent: Mon, Nov 22, 2010 4:11 pm
> Subject: [R] how to loop through variables in R?
>
>
>
>
> After importing a table with "M" variables and "N" records, I'd like to
>
> calculate chi-square statistics, say, between N1, N2; N1, N3, ..., N1, Ni,
>
> and then N2, N3, ... N2, Ni, ..., Ni-1, Ni.  Two loops should be ok but the
>
> manual & online help don't show a systematic way to do so but instead show
>
> hard-code examples so users have to type in the names themselves one by
>
> one...
>
>  Sorry for typo.  It should be "N" variables and "M" records. 
>
> Thank you very much.
>   
Assuming you are working with a data frame df, and your variable with 
the name of a column in it is col1, you should be able to extract that 
column as a vector using df[[col1]]. And yes, "the manual" does describe 
this notation.



More information about the R-help mailing list