0

I am playing around some concept with R. This question is an extension of what I asked earlier here

I have a data as below

V1   V2 V3
..   1  1
..   2  1
..   1  2
..   3  2

As in the earlier question, I would like to calculate variance of V1 for each value of V2 cumulatively. However, I need to split the dataset according to values of V3 and calculate the variance of each individual datasets. I can do this using split function and then use the technique described in this answer. Is there a better way to achieve the same.

Community
  • 1
  • 1
hardikudeshi
  • 1,441
  • 5
  • 18
  • 22
  • So you essentially want to do the same thing as in the earlier question, but for every unique combination of `V2` and `V3` instead of `V2` alone? – Backlin Sep 18 '12 at 12:05
  • No, I want to split the data frame according to `V3` and calculate the cumulative variance for each of the resulting datasets. – hardikudeshi Sep 18 '12 at 12:08

1 Answers1

3
# Ben Bolker's solution to your earlier question
bens.func <- function(d){
    u <- sort(unique(d$V2))
    ans <- sapply(u,function(x) {
        with(d,var(V1[V2<=x]))
    })
    names(ans) <- u
    ans
}

# Your new dataset
d <- data.frame(V1=runif(1000),V2=sample(1:10,size=1000,replace=TRUE),
                V3=sample(1:10,size=1000,replace=TRUE))

# Split and lapply takes care of the rest
lapply(split(d, d$V3), bens.func)
Backlin
  • 14,612
  • 2
  • 49
  • 81