[R] flexible alternative to subsetting dataframe inside nested loops

mgierdal mgierdal at gmail.com
Wed Mar 24 14:52:48 CET 2010


I have a dataFrame variable:
  
      L1  L2 L3 ... v1 v2 ...
  1
  2
  3
  4
  ...

I want to process subsets of it as defined by combinations of L1-L2-L3. I do
it successfully using nested loops:

for (i in valuesOfL1 {
  for (j in valuesOfL2) {
    for (k in valuesOfL3) {
      tempData <- subset(dataFrame, (L1 == i & L2 == j  & L3 == k,
select=c(v1,v2) ))
      if (dim(tempData)[1]>0) # does this combination of L1-L2-L3 contain
any data?
        { process(tempData) }
    }
   }
  }

It works fine but:
1. I'm sure there can be more efficient solution
2. each time I want to change criteria (i.e. subset by less or more Ls) I
have to manually change number of nested loops in my code.

Is there a more flexible way to do it?

Thanks
-- 
View this message in context: http://n4.nabble.com/flexible-alternative-to-subsetting-dataframe-inside-nested-loops-tp1680580p1680580.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list