计算R中数据集的多重方差 [英] Computing multiple variance of a dataset in R

查看:118
本文介绍了计算R中数据集的多重方差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与与此有关问题.

我的数据如下

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

我需要为V2的每个值累计计算V1中数据的方差(这意味着对于V2的特定值,例如nV1的所有行都具有对应的必须小于n.

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.

在这种情况下ddply会提供帮助吗?

Will ddply help in such a case?

推荐答案

我不认为ddply会有所帮助,因为它基于获取数据的不重叠子集的概念框架.

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 ...

这篇关于计算R中数据集的多重方差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆