I have a database where every day is repeated several times, so there are several rows for the same date. (Btw, I use the package lubridate).
What I want to do is :
Create a column T1 and a column T2 of the first lowest and first highest price value. T1 would return blank/NA cells except for the rows where it finds the first highest and lowest prices. However, and this is where I am stuck, I want it to consider the duplicates together. So it will be like a loop : for the first set of duplicates, find T1 and T2, then move to the second set of duplicates, etc....
newdf4<-Data %>%
mutate(T1= max(which(settle < 120)))%>%
mutate(T2=min(which(settle> 120)))
Here is how my data looks like:
<date> <dttm> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <lgl> <lgl>
1 2002-01-02 2002-01-10 00:00:00 118 125 125 125. 125. 55 NA NA
2 2002-01-02 2002-03-11 00:00:00 125 NA NA NA NA 0 NA NA
3 2002-01-02 2002-05-10 00:00:00 128 NA NA NA NA 0 NA NA
4 2002-01-02 2002-07-10 00:00:00 127 NA NA NA NA 0 NA NA
5 2002-01-02 2002-09-10 00:00:00 130 NA NA NA NA 0 NA NA
6 2002-01-02 2002-11-11 00:00:00 180 120 120 120 120 5 NA NA
Thanks a lot in advance.
EDIT :
dput(head(Data))
structure(list(Date = structure(c(11689, 11689, 11689, 11689,
11689, 11689), class = "Date"), Echeance = structure(c(1010620800,
1015804800, 1020988800, 1026259200, 1031616000, 1036972800), class =
c("POSIXct", "POSIXt"), tzone = "UTC"), Settle = c(118, 125, 128, 127,
130, 180), Open = c(125, NA, NA, NA, NA, 120), Haut = c(125,
NA, NA, NA, NA, 120), Bas = c(124.75, NA, NA, NA, NA, 120), Close =
c(124.75, NA, NA, NA, NA, 120), Vol_Q = c(55, 0, 0, 0, 0, 5), Bloc_Q = c(NA,
NA, NA, NA, NA, NA), Trades = c(NA, NA, NA, NA, NA, NA), `Vol_€` =
c(343062.5,
0, 0, 0, 0, 30000), O.I. = c(908, 3645, 1603, 100, 157, 1210)), row.names =
c(NA,-6L), class = c("tbl_df", "tbl", "data.frame"))