I am trying to extract the start and end time index separately for all the labels and store them separately.
As suggested in the comment I prepared an example dataset
data <- rnorm(11)
dates1 <- as.POSIXct("2019-03-18 10:30:00", tz = "CET") + 0:6*60
dates2 <- as.POSIXct("2019-03-19 08:30:00", tz = "CET") + 0:3*60
dates <- append(dates1, dates2)
R <- xts(x = data, order.by = dates)
colnames(R) <- "R"
R$Label[1:7] <- 1
R$Label[8:11] <- 2
Output:
R Label
2019-03-18 10:30:00 1.193363635 1
2019-03-18 10:31:00 -0.558021057 1
2019-03-18 10:32:00 0.670440862 1
2019-03-18 10:33:00 0.073794492 1
2019-03-18 10:34:00 -0.416108940 1
2019-03-18 10:35:00 -0.596981420 1
2019-03-18 10:36:00 0.002006772 1
2019-03-19 08:30:00 -1.245200719 2
2019-03-19 08:31:00 0.417944923 2
2019-03-19 08:32:00 1.699169683 2
2019-03-19 08:33:00 0.861448103 2
Class of R is xts, zoo.
Now I would like to store the start and end time index for label 1 and two separately. I have many more data with more labels, so it needs to be automated. I would really appreciate if you can help. Thank you
Using the data you have posted:
library(xts)
library(dplyr)
library(tibble)
set.seed(42)
data <- rnorm(11)
dates1 <- as.POSIXct("2019-03-18 10:30:00", tz = "CET") + 0:6*60
dates2 <- as.POSIXct("2019-03-19 08:30:00", tz = "CET") + 0:3*60
dates <- append(dates1, dates2)
R <- xts(x = data, order.by = dates)
colnames(R) <- "R"
R$Label <- 1 # note I have removed the indexing here
R$Label[8:11] <- 2
R %>%
as.data.frame() %>%
rownames_to_column() %>%
group_by(Label) %>%
summarise(min = min(rowname), max = max(rowname) )
# A tibble: 2 x 3
Label min max
<dbl> <chr> <chr>
1 1 2019-03-18 09:30:00 2019-03-18 09:36:00
2 2 2019-03-19 07:30:00 2019-03-19 07:33:00