mgierdal
2010-Mar-24 13:52 UTC
[R] flexible alternative to subsetting dataframe inside nested loops
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.
hadley wickham
2010-Mar-24 13:59 UTC
[R] flexible alternative to subsetting dataframe inside nested loops
On Wed, Mar 24, 2010 at 8:52 AM, mgierdal <mgierdal at gmail.com> wrote:> > 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.?split library(plyr) ?ddply Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/