[R] Complicated For Loop (to me)

agm. amurray at vt.edu
Mon Nov 9 21:18:16 CET 2009


I've looked through ?split and run all of the code, but I am not sure that I
can use it in such a way to make it do what I need.  Another suggestion was
using "lists", but again, I am sure that the process can do what I need, but
I am not sure it would work with so many observations.

I might have been too simple in my code.  Let me try to explain it more
clearly:

I've got a data set of 4500 observations.  I have already subset it into
race/ethnicity (which I did by simple code).  Now I needed to subset each
race/ethnicity again into 9 separate regions.  I again did this by simple
code.

The problem is now, I need to calculate a percentage for three different
variables for all 9 regions for each race.  I was trying to do this through
a loop command.

So a snippet of my code is :

names <- c("white", "black", "asian", "hispanic")
for(j in 1:length(names)){
for(i in 1:9){
names[j].cd[i].es.wash <- subset(names[j].cd[i], SLUNCH==1)
es.cd[i].names.w <-
sum(names.cd[i].es.wash$NWEIGHT)/sum(names.cd[i]$NWEIGHT)
}
}


Maybe that makes it clearer.  If not, I apologize.  Thanks for the help that
I have already received.  It is greatly appreciated.

Tony

-- 
View this message in context: http://old.nabble.com/Complicated-For-Loop-%28to-me%29-tp26269479p26272994.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list