This is a followup question to Counting peaks in r per group.
Reproducible data:
set.seed(949494)
Happiness <- round(runif(100, -100, 100))
ID <- rep(c("ID1", "ID2", "ID3", "ID4", "ID5"), 20)
Stimuli <- rep(1:4, 1)
DF <- data.frame(ID, Stimuli, Happiness)
Function to calculate the Happiness threshold per ID by using each ID's unique sd():
# 1SD
f.SD1 <- function(y) {
SD1_thresh <- mean(y) + (1*sd(y))
return(SD1_thresh)
}
Function to identify when Happiness is above (TRUE = 1) or below (FALSE = 0) the threshold:
# SD1
f.Peaks_SD1 <- function(X, thresh) {
H_peaks_1 <- ifelse(X >= thresh ,1,0)
return(H_peaks_1)
}
Now I want to group by ID and Stimuli so that I can determine average peaks per stimuli:
H_peaks_1_df <- DF %>% group_by(Stimuli, ID) %>% summarise(thresh_SD1 = f.SD1(Happiness), ttime = sum(Happiness > thresh_SD1), nP_H_SD1 = sum(diff(c(f.Peaks_SD1(Happiness, thresh = thresh_SD1), 0)) < 0))
H_peaks_1_df
summary(H_peaks_1_df)
Output:
The problem with this output is that the thresholds for the same ID are different because the sd() was calculated per ID per Stimuli. I want to calculate the sd() across all Stimuli per ID and then count peaks per Stimuli. So, the output H_peaks_1_df here is perfect (group_by(Stimuli, ID)), just the column "thresh_SD1" should be the same value for ID1, namely "58.5" which is correctly calculated when grouping only by ID.
Is it possible in dplyr to execute the "thresh_SD1" calculation via group_by(ID) and then count peaks and total time via group_by(Stimuli, ID) in simple code?
Thanks in advance!