I need to date and time of the data that satisfies a condition in ifelse.
Glucose_4h <- character()
for(i in 1:96){
A <- unlist(difftime(Time_Meal[i], Time_Glucose[1:15372], units="mins"))
B <- ifelse(as.numeric(A)>0 & as.numeric(A)<5.0, "Inc_G","Exc_G")
Glucose_4h <- c(Glucose_4h,B)
}
In this code I also need which Time_Glucose satisfied the condition Could you help me about this?
So you need the values of Time_Glucose
where Glucose_4h
is equal to "Inc_G"
? In your example B
will always be 15372 long (the length of Time_Glucose
. You could then capture that along with what you're currently doing:
res <- NULL
for(i in 1:96){
A <- unlist(difftime(Time_Meal[i], Time_Glucose[1:15372], units="mins"))
B <- ifelse(as.numeric(A)>0 & as.numeric(A)<5.0, "Inc_G","Exc_G")
res <- rbind(res, data.frame(obs = i, Glucose_4h = B, Time_Glucose = Time_Glucose))
}
Then, if you only want the ones where Glucose_4h == "Inc_G"
, you could do the following:
res[which(res$Glucose_4h == "Inc_G"), ]