I have a time series of data (Q) and would like to create a column (Category) that labels the daily occurrence of min Q and max Q with labels Min and Max, respectively. The code below, identifies the Time when min (Time_of_min_Q) and max (Time_of_max_Q) occur in the day. I use the same technique to add corresponding labels but it does not work. Your suggestions are appreciated.
library(dplyr)
set.seed(12345)
dateTime <- seq(as.POSIXct("2005-01-01 00:00:00", tz = "GMT"),
as.POSIXct("2005-01-03 00:00:00", tz = "GMT"),
by = 60*15)
Q <- sample(100:395,193,rep=TRUE)
df <- data.frame(dateTime,Q)
df$Time<-format(as.POSIXct(dateTime), format = "%H:%M:%S")
df1<-df %>%
group_by(Date = as.Date(dateTime)) %>%
mutate(Time_of_max_Q = Time[which.max(Q)],
Time_of_min_Q = Time[which.min(Q)],
Category = ifelse(Time[which.max(Q)],"Max",
ifelse(Time[which.min(Q)],"Min",NA)))
CodePudding user response:
The condition in ifelse
is not logical. It may be created as logical with ==
. According to ?ifelse
test - an object which can be coerced to logical mode.
library(dplyr)
df %>%
group_by(Date = as.Date(dateTime)) %>%
mutate(Time_of_max_Q = Time[which.max(Q)],
Time_of_min_Q = Time[which.min(Q)],
Category = ifelse(Q== max(Q),"Max",
ifelse(Q == min(Q),"Min",NA))) %>%
ungroup
If there are ties for max/min
values, then subset the Time
(which.max/which.min
returns the index of first max/min
values respectively) and use ==
with the original 'Time' column
df %>%
group_by(Date = as.Date(dateTime)) %>%
mutate(Time_of_max_Q = Time[which.max(Q)],
Time_of_min_Q = Time[which.min(Q)],
Category = ifelse(Time[which.max(Q)] == Time,"Max",
ifelse(Time[which.min(Q)] == Time,"Min",NA)))