mercredi 1 juillet 2020

Keeping maximum and minimum values in a logical sequence

Consider this data:

df <- structure(list(Date = structure(c(2922, 4018, 5113, 7305, 8035, 
12053, 14975, 16436, 17532, 17897), class = "Date"), HAM = c(1016.89391375364, 
-1269.0910012255, -1097.9927692669, -5069.52785909119, 3168.39687262048, 
-1265.24208195278, -1218.5560466457, 1463.67252927616, 1259.20509267793, 
1267.89637533522), State = c("Expansion", "Contraction", "Contraction", 
"Contraction", "Expansion", "Contraction", "Contraction", "Expansion", 
"Contraction", "Expansion"), sd = c("larger", "smaller", "smaller", 
"smaller", "larger", "smaller", "smaller", "larger", "larger", 
"larger")), row.names = c(NA, -10L), class = "data.frame")

I would like to keep a logical sequence of "Expansion" followed by "Contraction", taking into consideration the maximum value (for Expansion) and minimum value (for "Contraction"). Expected result is this data frame:

ndf <- structure(list(Date = structure(c(2922, 7305, 8035, 12053, 16436
), class = "Date"), HAM = c(1016.89391375364, -5069.52785909119, 
3168.39687262048, -1265.24208195278, 1463.67252927616), State = c("Expansion", 
"Contraction", "Expansion", "Contraction", "Expansion"), sd = c("larger", 
"smaller", "larger", "smaller", "larger")), row.names = c(1L, 
4L, 5L, 6L, 8L), class = "data.frame")

Aucun commentaire:

Enregistrer un commentaire