Discussion:
[R] Time outside limits
Bart Joosen
2014-10-16 18:12:53 UTC
Permalink
Hi,
I'm currently facing the problem that I need to write a function where I get a dataframe back which contains the time (in hours) outside the limits of a temperature sensor, each month, and for how long exactly.
I wrote a for loop which check:- if a datapoint is outside the limit- if the previous datapoint is outside the limt, then count + 1- if the next datapoint isn't outside: write in dataframe.
I guess this could be with some vectorisation function, I tried with seq_along, and match, but couldn't figure it out.
Here some sample data:
y <- c(rnorm(10,25), rnorm(10,32),rnorm(10,25), rnorm(10,20), rnorm(10,25))x <- seq(c(ISOdate(2000,3,20)), by = "hour", length.out = length(y))
limits of y: c(22,27)
Thanks
Bart
[[alternative HTML version deleted]]
daniel
2014-10-16 18:44:58 UTC
Permalink
Bart,

Check if the following could help you.

library(xts)
y <- c(rnorm(10,25), rnorm(10,32),rnorm(10,25), rnorm(10,20),
rnorm(10,25)); x <- seq(c(ISOdate(2000,3,20)), by = "hour", length.out =
length(y))
z <- xts( y, order.by=as.POSIXct(x))
limit <- ifelse( lag(z) < 22 | z > 27, 1, 0)


Daniel Merino
Post by Bart Joosen
Hi,
I'm currently facing the problem that I need to write a function where I
get a dataframe back which contains the time (in hours) outside the limits
of a temperature sensor, each month, and for how long exactly.
I wrote a for loop which check:- if a datapoint is outside the limit- if
the previous datapoint is outside the limt, then count + 1- if the next
datapoint isn't outside: write in dataframe.
I guess this could be with some vectorisation function, I tried with
seq_along, and match, but couldn't figure it out.
y <- c(rnorm(10,25), rnorm(10,32),rnorm(10,25), rnorm(10,20),
rnorm(10,25))x <- seq(c(ISOdate(2000,3,20)), by = "hour", length.out =
length(y))
limits of y: c(22,27)
Thanks
Bart
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--
Daniel

[[alternative HTML version deleted]]
Clint Bowman
2014-10-16 20:36:00 UTC
Permalink
?rle

Clint Bowman INTERNET: clint at ecy.wa.gov
Air Quality Modeler INTERNET: clint at math.utah.edu
Department of Ecology VOICE: (360) 407-6815
PO Box 47600 FAX: (360) 407-7534
Olympia, WA 98504-7600

USPS: PO Box 47600, Olympia, WA 98504-7600
Parcels: 300 Desmond Drive, Lacey, WA 98503-1274
Post by daniel
Bart,
Check if the following could help you.
library(xts)
y <- c(rnorm(10,25), rnorm(10,32),rnorm(10,25), rnorm(10,20),
rnorm(10,25)); x <- seq(c(ISOdate(2000,3,20)), by = "hour", length.out =
length(y))
z <- xts( y, order.by=as.POSIXct(x))
limit <- ifelse( lag(z) < 22 | z > 27, 1, 0)
Daniel Merino
Post by Bart Joosen
Hi,
I'm currently facing the problem that I need to write a function where I
get a dataframe back which contains the time (in hours) outside the limits
of a temperature sensor, each month, and for how long exactly.
I wrote a for loop which check:- if a datapoint is outside the limit- if
the previous datapoint is outside the limt, then count + 1- if the next
datapoint isn't outside: write in dataframe.
I guess this could be with some vectorisation function, I tried with
seq_along, and match, but couldn't figure it out.
y <- c(rnorm(10,25), rnorm(10,32),rnorm(10,25), rnorm(10,20),
rnorm(10,25))x <- seq(c(ISOdate(2000,3,20)), by = "hour", length.out =
length(y))
limits of y: c(22,27)
Thanks
Bart
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--
Daniel
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Loading...