Calculating cumulative standard deviation by group using R

前端 未结 2 653
闹比i
闹比i 2021-01-14 12:29

I am pretty new to R and wanted to calculate the cumulative standard deviation by group in R. I have a data frame D which has an ID for visitor and the corresponding time on

相关标签:
2条回答
  • 2021-01-14 12:52

    You can do it with base functions:

    cumsd <- function(x) sapply(sapply(seq_along(x), head, x=x), sd)
    df1$cum_sd <- ave(df1$top, df1$ID, FUN=cumsd)
    
    0 讨论(0)
  • 2021-01-14 13:04

    We can use runSD from TTR. Convert the 'data.frame' to 'data.table' (setDT(df1)), grouped by 'ID', we apply the runSD on the 'top' column and assign (:=) the output to create the 'cum_sd'.

    library(data.table)
    library(TTR)
    setDT(df1)[, cum_sd := round(runSD(top, n=1, cumulative=TRUE),2) ,ID]
    df1
    #  ID  top cum_sd
    #1: v1  2.3     NA
    #2: v1  4.8   1.77
    #3: v1 10.2   4.04
    #4: v2 16.2     NA
    #5: v2 12.2   2.83
    #6: v2 14.3   2.00
    #7: v2 12.4   1.87
    #8: v3  8.2     NA
    #9: v3  8.8   0.42
    
    0 讨论(0)
提交回复
热议问题