Reputation: 3760
I need to filter the Time Series data based on groups. However filtering has to be done at the beginning (-5 minutes) and end of each group (-2 minutes), it means i would like to remove rows at the beginning (-5 minutes) and end (-2 minutes) of each group.
Here is the sample code:
Time <- c("2015-08-21T10:00:51", "2015-08-21T10:02:51", "2015-08-21T10:04:51", "2015-08-21T10:06:51",
"2015-08-21T10:08:51", "2015-08-21T10:10:51","2015-08-21T10:12:51", "2015-08-21T10:14:51",
"2015-08-21T10:16:51", "2015-08-21T10:18:51", "2015-08-21T10:20:51", "2015-08-21T10:22:51")
x <- c(38.855, 38.664, 40.386, 40.386, 40.195, 40.386, 40.386, 40.195, 40.386, 38.855, 38.664, 40.386)
y <- c("a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b")
data <- data.frame(Time,x,y)
data$Time <- as.POSIXct(data$Time, format = "%Y-%m-%dT%H:%M:%S")
Y columns show us the groups, which in this particular case is a and b
So for this example i would remove 3 first rows and 2 last rows for level a, for b same thing (in my original data it will not be that easy to remove it according to the row counts). So what i would get at the end something like this:
Time x y
4 2015-08-21 10:06:51 40.386 a
10 2015-08-21 10:18:51 38.855 b
I wanna point out that this is only sample data!
Thanks for help!
I would rather filter the data based on time column rather than row counts, my original data is not so nicely structured like this one and number of rows per each group vary.
Upvotes: 2
Views: 581
Reputation: 70623
What about this? Split the data.frame, find first five and last two minutes, do some logical looking up of rows and output the result.
xy <- split(data, data$y)
xy <- lapply(xy, FUN = function(m) {
m[(m$Time > min(m$Time) + (5 * 60)) & ((max(m$Time) - (2 * 60)) > m$Time), ]
})
do.call("rbind", xy)
Time x y
a 2015-08-21 10:06:51 40.386 a
b 2015-08-21 10:18:51 38.855 b
I understand it's cool these days to also present a dplyr
solution. So here it is.
library(dplyr)
data %>%
group_by(y) %>%
filter((Time > (min(Time) + (5*60))) & (max(Time) - (2*60) > Time))
Upvotes: 1