Home > OS >  Count Peaks with R
Count Peaks with R

Time:07-26

(A big thank you to all the comments so far, especially by enter image description here

I am using the following fake data (inspired by enter image description here

Goal Summary table 2: enter image description here

The same will also be for peaks below negative threshholds.

Step 1 (inspired by enter image description here

2 - sum all the "1"s to get a total time above threshholds. (I am struggling to bring a matrix back into vector of dataframe)

Thankful for any tips & guidance!

CodePudding user response:

Creating your own data is not as difficult as it may seem. This made up data seems to represent your problem. If not, you can edit your question to provide more details and your own data:

set.seed(42)
Happiness <- round(runif(30, 0, 100))
ID <- rep(1:2, 15)
DFR <- data.frame(ID, Happiness)

DFR is a data frame with two columns, ID and Happiness. Now to analyze each ID separately we need to split the data frame:

DFR.ID <- split(DFR, DFR$ID)

DFR.ID is a list containing two data frames, one for each ID.

low_thresh <- 20
med_thresh <- 50
high_thresh <- 70
Thresh <- function(X) {
    V_peaks_1a <- ifelse(X >= low_thresh ,1,0)
    V_peaks_2a <- ifelse(X >= med_thresh ,1,0)
    V_peaks_3a <- ifelse(X >= high_thresh ,1,0)
    return(cbind(V_peaks_1a, V_peaks_2a, V_peaks_3a))
}

Now we create a function called Thresh to analyze Happiness and return a matrix with three columns, one for each threshold. Finally we use the function on each ID and produce a list containing a matrix for each ID showing the changes in Happiness:

V_peaks.ID <- lapply(DFR.ID, function(id) Thresh(id$Happiness))
V_peaks.ID
# $`1`
#       V_peaks_1a V_peaks_2a V_peaks_3a
#  [1,]          1          1          1
#  [2,]          1          0          0
#  [3,]          1          1          0
#  [4,]          1          1          1
#  [5,]          1          1          0
#  [6,]          1          0          0
#  [7,]          1          1          1
#  [8,]          1          0          0
#  [9,]          1          1          1
# [10,]          1          0          0
# [11,]          1          1          1
# [12,]          1          1          1
# [13,]          0          0          0
# [14,]          1          0          0
# [15,]          1          0          0
# 
# $`2`
#       V_peaks_1a V_peaks_2a V_peaks_3a
#  [1,]          1          1          1
#  [2,]          1          1          1
#  [3,]          1          1          0
#  [4,]          0          0          0
#  [5,]          1          1          1
#  [6,]          1          1          1
#  [7,]          1          0          0
#  [8,]          1          1          1
#  [9,]          0          0          0
# [10,]          1          1          0
# [11,]          0          0          0
# [12,]          1          1          1
# [13,]          1          1          0
# [14,]          1          1          1
# [15,]          1          1          1
  • Related