Efficient calculation of matrix cumulative standard deviation in r

前端 未结 2 646
一个人的身影
一个人的身影 2020-12-01 19:45

I recently posted this question on the r-help mailing list but got no answers, so I thought I would post it here as well and see if there were any suggestions.

I am

2条回答
  •  -上瘾入骨i
    2020-12-01 20:31

    Another try (Marek's is faster)

    cumsd2 <- function(y) {
    n <- nrow(y)
    apply(y,2,function(i) {
        Xmeans <- lapply(1:n,function(z) rep(sum(i[1:z])/z,z))
        Xs <- sapply(1:n, function(z) i[1:z])
        sapply(2:n,function(z) sqrt(sum((Xs[[z]]-Xmeans[[z]])^2,na.rm = T)/(z-1)))
    })
    }
    

提交回复
热议问题