[R] Complicated For Loop (to me)

John Kane jrkrideau at yahoo.ca
Tue Nov 10 00:33:17 CET 2009


I think that we probably need a sample database of your original data.  
A few lines of the dataset would probably be enough as long as it was fairly representative of the overall data set.  See ?dput for a way of conveniently supply a sample data set.

Otherwise off the top of my head, I would think that you could just put all your subsets into a list and use lapply  but I'm simply guessing without seeing the data.

--- On Mon, 11/9/09, agm. <amurray at vt.edu> wrote:

> From: agm. <amurray at vt.edu>
> Subject: Re: [R] Complicated For Loop (to me)
> To: r-help at r-project.org
> Received: Monday, November 9, 2009, 3:18 PM
> 
> 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.
> 


      __________________________________________________________________
Make your browsing faster, safer, and easier with the new Internet Explorer® 8. Optimized for Yahoo! Get i



More information about the R-help mailing list