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