Quite a specific question...but here it goes.
I have a vector of data indicating thermal change, and from it would like to know whether something has been switched 'ON' or 'OFF'. This something always starts 'OFF'. A threshold of +1.5 degrees indicates it has been switched 'ON', and stays on until a threshold of -1.5 degrees indicates it has been switched 'OFF' again, reaching high or low plateaus in between with much smaller +/- change. I have a numeric vector of positive and negative values that I would like to turn into a character or factor with levels 'ON' and 'OFF' in the appropriate places.
Using this:
> Data$delta
[1] 0.02 0.00 0.04 -0.06 -0.06 -0.02 0.01 0.31 0.22 0.21 -0.09 -0.02 0.03 0.02 0.01
[16] 0.00 0.02 0.03 0.03 0.04 0.04 0.01 0.00 0.01 0.02 0.05 0.04 0.04 0.01 0.04
[31] 0.02 0.01 -0.03 0.00 0.03 0.03 0.03 0.04 0.04 0.02 0.02 0.01 0.02 -0.02 -0.01
[46] -0.03 0.01 0.03 0.37 0.14 0.04 -0.34 -0.15 -0.07 0.00 0.01 0.29 0.03 0.26 -0.12
[61] 0.05 -0.02 -0.03 0.10 -0.11 -0.01 -0.07 -0.03 -0.01 0.01 0.30 0.12 0.05 -0.25 -0.06
[76] -0.04 -0.04 -0.07 -0.02 0.01 0.04 0.02 0.03 -0.07 -0.12 -0.18 -0.12 -0.08 -0.05 -0.04
[91] 0.34 2.99 4.29 5.00 1.83 -0.51 -1.63 -0.33 2.62 -0.38
I create an empty vector to receive the for() loop output and insert 'OFF' as switch is always starting with 'OFF'
> Data$switch<- as.character(NA)
> Data$switch<-as.character(c("OFF",Data$switch[2:(length(Data$switch))]))
> head(Data$switch
[1] "OFF" NA NA NA NA NA
I then create my nested ifelse() for() loop, accounting for the fact row 1 of Data$switch is already complete...
> for(i in (2:(length(Data$delta))))
{ ifelse(Data$switch[1:((length(Data$switch))-1)] == "OFF",
(ifelse (Data$delta[i] < 1.5 , Data$switch[i] <- "OFF", Data$switch[i] <- "ON" )),
(ifelse (Data$delta[i] > {-1.5} , Data$switch[i] <- "ON", Data$switch[i] <- "OFF")))
}
Which returns...
> Data$switch
[1] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[16] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[31] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[46] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[61] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[76] "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF" "OFF"
[91] "OFF" "ON" "ON" "ON" "ON" "ON" "OFF" "ON" "ON" "ON"
Which all appears fine...apart from value 98, which should not have switched to 'ON' with delta only '-0.33'. On a larger data set these seem to be regular false change from OFF to ON at values <1.5? I've tried the syntax again in multiple forms but these are worse not better. Can anyone help identify the problem please? Data$delta is a numeric vector.