[R] Complicated For Loop (to me)
jim holtman
jholtman at gmail.com
Tue Nov 10 01:50:52 CET 2009
Since you did not follow the posting and provide data, here is a way
that you can split by race & region and perform operations on each
subset of the data frame:
> # test data
> n <- 4500
> x <- data.frame(race=sample(c('a','b','c'), n, TRUE),
+ region=sample(1:9, n, TRUE), values=runif(n))
> # split into race & region
> x.s <- split(x, list(x$race, x$region), drop=TRUE)
> # average for each subset
> sapply(x.s, function(.df) mean(.df$values))
a.1 b.1 c.1 a.2 b.2 c.2 a.3
b.3 c.3 a.4
0.4832214 0.4792623 0.4846138 0.5098792 0.4997666 0.5212905 0.5068535
0.5140438 0.5580490 0.4849455
b.4 c.4 a.5 b.5 c.5 a.6 b.6
c.6 a.7 b.7
0.4952947 0.4639553 0.4798192 0.5030650 0.4889465 0.4839610 0.5080325
0.5176548 0.4788206 0.5300395
c.7 a.8 b.8 c.8 a.9 b.9 c.9
0.4799252 0.4871341 0.4675535 0.4899783 0.5172642 0.4968789 0.5499326
>
On Mon, Nov 9, 2009 at 3:18 PM, agm. <amurray at vt.edu> wrote:
>
> 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.
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list