I posted this earlier but I think I wasn't too clear with my question so I am updating it and included the progress I made.
I have a data where I obtained local maxima designated by a TRUE
value in my loc_max
column. I would like to identify my "true maxima" from these local maxima based on the following conditions: 1.) the 5 values preceding a local maxima must have at least 3 positive first derivative; AND 2.) the succeeding 5 values must have at least 3 negative first derivative. Here's my sample data frame:
val <- c(0.06796823, 0.12165540, 0.17685980, 0.28518490, 0.36616820,
0.40935790, 0.45418170, 0.48220730, 0.45214280, 0.40290130,
0.38103100, 0.39525690, 0.40527800, 0.48172680, 0.54250300,
0.56136270, 0.53755350, 0.57047540, 0.55738850, 0.50470080,
0.47487730, 0.45653140, 0.45670750, 0.43722310, 0.42154800,
0.41154490, 0.38138090, 0.41802160, 0.42043370, 0.39982040,
0.35258890, 0.32990900, 0.28508770, 0.23949280, 0.19405640,
0.16321880, 0.17098540, 0.17572110, 0.17464730, 0.17670690,
0.16105620, 0.18609890, 0.19083090, 0.19506300, 0.16865580,
0.15830920)
loc_max <- c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE,
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE,
FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE,
FALSE)
I obtained the first derivative using a simple method of subtracting each value from the previous as given in the following code:
df <- data.frame(val, loc_max)
df2 <- df %>%
mutate(first_diff = val - lag(val))
From there, I got the indices of my local maxima using this code:
locmax_ind <- c(which(df2$loc_max == "TRUE"))
Now, this is where I am having a problem... When I subset the 5 preceding and succeeding values to satisfy my conditions, I wrote this code:
ifelse(sum(df2$first_diff[(locmax_ind - 5):(locmax_ind - 1)] > 0) >= 3 &
sum(df2$first_diff[((locmax_ind + 1):(locmax_ind + 5))] < 0) >= 3, TRUE, NA)
It gives me the only the first used
warning and can't seem to extract the other elements in results.
My end goal is to produce a data frame that looks like this:
val loc_max first_diff true_max
1 0.06796823 FALSE NA NA
2 0.12165540 FALSE 0.05368717 NA
3 0.17685980 FALSE 0.05520440 NA
4 0.28518490 FALSE 0.10832510 NA
5 0.36616820 FALSE 0.08098330 NA
6 0.40935790 FALSE 0.04318970 NA
7 0.45418170 FALSE 0.04482380 NA
8 0.48220730 TRUE 0.02802560 TRUE
9 0.45214280 FALSE -0.03006450 NA
10 0.40290130 FALSE -0.04924150 NA
11 0.38103100 FALSE -0.02187030 NA
12 0.39525690 FALSE 0.01422590 NA
13 0.40527800 FALSE 0.01002110 NA
14 0.48172680 FALSE 0.07644880 NA
15 0.54250300 FALSE 0.06077620 NA
16 0.56136270 TRUE 0.01885970 TRUE
17 0.53755350 FALSE -0.02380920 NA
18 0.57047540 TRUE 0.03292190 TRUE
19 0.55738850 FALSE -0.01308690 NA
20 0.50470080 FALSE -0.05268770 NA
21 0.47487730 FALSE -0.02982350 NA
22 0.45653140 FALSE -0.01834590 NA
23 0.45670750 TRUE 0.00017610 NA
24 0.43722310 FALSE -0.01948440 NA
25 0.42154800 FALSE -0.01567510 NA
26 0.41154490 FALSE -0.01000310 NA
27 0.38138090 FALSE -0.03016400 NA
28 0.41802160 FALSE 0.03664070 NA
29 0.42043370 TRUE 0.00241210 NA
30 0.39982040 FALSE -0.02061330 NA
31 0.35258890 FALSE -0.04723150 NA
32 0.32990900 FALSE -0.02267990 NA
33 0.28508770 FALSE -0.04482130 NA
34 0.23949280 FALSE -0.04559490 NA
35 0.19405640 FALSE -0.04543640 NA
36 0.16321880 FALSE -0.03083760 NA
37 0.17098540 FALSE 0.00776660 NA
38 0.17572110 TRUE 0.00473570 NA
39 0.17464730 FALSE -0.00107380 NA
40 0.17670690 TRUE 0.00205960 NA
41 0.16105620 FALSE -0.01565070 NA
42 0.18609890 FALSE 0.02504270 NA
43 0.19083090 FALSE 0.00473200 NA
44 0.19506300 TRUE 0.00423210 NA
45 0.16865580 FALSE -0.02640720 NA
46 0.15830920 FALSE -0.01034660 NA
I'm still confused with loops and any help is very much appreciated. Thanks!