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... -- View this message in context: http://r.789695.n4.nabble.com/how-to-loop-through-variables-in-R-tp3053214p3053214.html Sent from the R help mailing list archive at Nabble.com.
-----Original Message----- From: jas4710 <watashi@post.com> To: r-help@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. [[alternative HTML version deleted]]
-----Original Message----- From: Yuliya Matveyeva <yuliya.rmail@gmail.com> To: watashi@post.com Sent: Mon, Nov 22, 2010 4:32 pm Subject: Re: [R] how to loop through variables in R? If you want to have a name-specific loop. Assign names to your variables after inserting them into the data.frame like that: colnames(df) <- c("var1","var23","var456","var44",...) for (nam in colnames(df)) { myfunction(df[[nam]]) } Data.frames support access by names. Unfortunately, in this sense I have to type in 1000 times... isn't there any function that allows retrieving and assigning all columns automatically? read.table in "the R intro manual" just brief says it can read a table but then there's no follow up about how to access the data.... [[alternative HTML version deleted]]