[R] flexible alternative to subsetting dataframe inside nested loops

hadley wickham h.wickham at gmail.com
Wed Mar 24 14:59:11 CET 2010


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/



More information about the R-help mailing list