hardikudeshi
hardikudeshi

Reputation: 1491

Computing multiple variance of a dataset in R

My problem is somewhat related to this question.

I have a data as below

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

I need to calculate variance of data in V1 for each value of V2 cumulatively (This means that for a particular value of V2 say n,all the rows of V1 having corresponding V2 less than n need to be included.

Will ddply help in such a case?

Upvotes: 2

Views: 216

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226332

I don't think ddply will help since it is built on the concept of taking non-overlapping subsets of a data frame.

d <- data.frame(V1=runif(1000),V2=sample(1:10,size=1000,replace=TRUE))
u <- sort(unique(d$V2))
ans <- sapply(u,function(x) {
    with(d,var(V1[V2<=x]))
})
names(ans) <- u

I don't know if there's a more efficient way to do this ...

Upvotes: 4

Related Questions