Home > OS >  How to create a new variable that retains the maximum value once it is reached in a sequence?
How to create a new variable that retains the maximum value once it is reached in a sequence?

Time:11-11

I have this sample data set:

library(dplyr)
library(ggplot2)

data.1 <-read.csv(text = "
sampling_date,value
3/4/2021,5
3/5/2021,5.181818182
3/6/2021,5.363636364
3/7/2021,5.545454545
3/8/2021,5.727272727
3/9/2021,5.909090909
3/10/2021,6.090909091
3/11/2021,6.272727273
3/12/2021,6.454545455
3/13/2021,6.636363636
3/14/2021,6.818181818
3/15/2021,7
3/16/2021,7.047619048
3/17/2021,7.095238095
3/18/2021,7.142857143
3/19/2021,7.19047619
3/20/2021,7.238095238
3/21/2021,7.285714286
3/22/2021,7.333333333
3/23/2021,7.380952381
3/24/2021,7.428571429
3/25/2021,7.476190476
3/26/2021,7.523809524
3/27/2021,7.571428571
3/28/2021,7.619047619
3/29/2021,7.666666667
3/30/2021,7.714285714
3/31/2021,7.761904762
4/1/2021,7.80952381
4/2/2021,7.857142857
4/3/2021,7.904761905
4/4/2021,7.952380952
4/5/2021,8
4/6/2021,8
4/7/2021,8
4/8/2021,7.9
4/9/2021,7.8
4/10/2021,7.7
4/11/2021,7.6
4/12/2021,7.5
4/13/2021,7.4
4/14/2021,7.3
4/15/2021,7.2
4/16/2021,7.1
4/17/2021,7
4/18/2021,7.090909091
4/19/2021,7.181818182
4/20/2021,7.272727273
4/21/2021,7.363636364
4/22/2021,7.454545455
4/23/2021,7.545454545
4/24/2021,7.636363636
4/25/2021,7.727272727
4/26/2021,7.818181818
4/27/2021,7.909090909
4/28/2021,8
4/29/2021,8
4/30/2021,8
5/1/2021,8
5/2/2021,8
") %>% mutate(sampling_date = as.Date(sampling_date, format = '%m/%d/%Y')) 

The plot shows that it reaches a maximum, then drops, and then it increases again.

ggplot(data.1) 
  geom_point(aes(x=sampling_date, y=value), shape = 0.5)

enter image description here

This is something anomalous for the process I am studying (once reached a maximum the value cannot decrease), so I wanted to replace the values after it reached the maximum with the maximum value. This happens in some samples but not in others, so if the maximum is reached and stays until the end of the sequence, no change should be made.

The output I need is this:

data.2 <-read.csv(text = "
sampling_date,value,value2
3/4/2021,5,5
3/5/2021,5.181818182,5.181818182
3/6/2021,5.363636364,5.363636364
3/7/2021,5.545454545,5.545454545
3/8/2021,5.727272727,5.727272727
3/9/2021,5.909090909,5.909090909
3/10/2021,6.090909091,6.090909091
3/11/2021,6.272727273,6.272727273
3/12/2021,6.454545455,6.454545455
3/13/2021,6.636363636,6.636363636
3/14/2021,6.818181818,6.818181818
3/15/2021,7,7
3/16/2021,7.047619048,7.047619048
3/17/2021,7.095238095,7.095238095
3/18/2021,7.142857143,7.142857143
3/19/2021,7.19047619,7.19047619
3/20/2021,7.238095238,7.238095238
3/21/2021,7.285714286,7.285714286
3/22/2021,7.333333333,7.333333333
3/23/2021,7.380952381,7.380952381
3/24/2021,7.428571429,7.428571429
3/25/2021,7.476190476,7.476190476
3/26/2021,7.523809524,7.523809524
3/27/2021,7.571428571,7.571428571
3/28/2021,7.619047619,7.619047619
3/29/2021,7.666666667,7.666666667
3/30/2021,7.714285714,7.714285714
3/31/2021,7.761904762,7.761904762
4/1/2021,7.80952381,7.80952381
4/2/2021,7.857142857,7.857142857
4/3/2021,7.904761905,7.904761905
4/4/2021,7.952380952,7.952380952
4/5/2021,8,8
4/6/2021,8,8
4/7/2021,8,8
4/8/2021,7.9,8
4/9/2021,7.8,8
4/10/2021,7.7,8
4/11/2021,7.6,8
4/12/2021,7.5,8
4/13/2021,7.4,8
4/14/2021,7.3,8
4/15/2021,7.2,8
4/16/2021,7.1,8
4/17/2021,7,8
4/18/2021,7.090909091,8
4/19/2021,7.181818182,8
4/20/2021,7.272727273,8
4/21/2021,7.363636364,8
4/22/2021,7.454545455,8
4/23/2021,7.545454545,8
4/24/2021,7.636363636,8
4/25/2021,7.727272727,8
4/26/2021,7.818181818,8
4/27/2021,7.909090909,8
4/28/2021,8,8
4/29/2021,8,8
4/30/2021,8,8
5/1/2021,8,8
5/2/2021,8,8
") %>% mutate(sampling_date = as.Date(sampling_date, format = '%m/%d/%Y'))

And the plot for both sets is as follows (red is the sequence I want)

ggplot(data.1) 
  geom_point(aes(x=sampling_date, y=value), shape = 0.5)  
  geom_point(aes(x=sampling_date, y=value2), data = data.2, shape = 1, color = "red", alpha=0.5)

enter image description here

This is what I tried but it creates the new variable all with the maximum value, which is 8.

data.1.new.version <- data.1 %>%
  mutate(value2 = if_else(sampling_date[]>which.max(data.1[,2]), max(value), value))

Any help would be appreciated.

CodePudding user response:

I think you're looking for cummax (cumulative maximum) in base R:

 data.1 %>% 
  mutate(value = cummax(value)) %>% 
  ggplot(aes(sampling_date, value))   
  geom_point()

enter image description here

  • Related