Search code examples
rtime-seriesxtszoo

how can we remove the rows from xts based on the seconds criteria


I have a problem. i want to delete some rows from the large data set. The problem is I have data of every 30 sec but I only want to go up to per minute. So, I want to remove the rows which have 30 sec init. for better understanding, I am attaching an example with the expected outcome that I want.

time                        value
2021-11-04 05:57:00         0.0
2021-11-04 05:57:30         0.0
2021-11-04 05:58:00         0.0
2021-11-04 05:58:30         0.0
2021-11-04 05:59:00         0.0
2021-11-04 05:59:30         0.0
2021-11-04 06:00:00         0.0
2021-11-04 06:00:30         0.0
2021-11-04 06:01:00         0.0
2021-11-04 06:01:30         0.0
2021-11-04 06:02:00         0.0
2021-11-04 06:02:30         0.0
2021-11-04 06:03:00         0.0
2021-11-04 06:03:30         0.0
2021-11-04 06:04:00         0.0
2021-11-04 06:04:30         0.0
2021-11-04 06:05:00         0.0
2021-11-04 06:05:30         0.0
2021-11-04 06:06:00         0.0
2021-11-04 06:06:30         0.0
2021-11-04 06:07:00         0.0
2021-11-04 06:07:30         0.0

what I want to be like that

time                        value
2021-11-04 05:57:00         0.0

2021-11-04 05:58:00         0.0

2021-11-04 05:59:00         0.0

2021-11-04 06:00:00         0.0

2021-11-04 06:01:00         0.0

2021-11-04 06:02:00         0.0

2021-11-04 06:03:00         0.0

2021-11-04 06:04:00         0.0

2021-11-04 06:05:00         0.0

2021-11-04 06:06:00         0.0

2021-11-04 06:07:00         0.0

every row with 30 sec init should be deleted from the data set.


Solution

  • You can first truncate the time and then remove duplicates. Since the 30 second elements are the non-unique elements, they get removed:

    library(xts)
    xts3 <- xts(x=rnorm(10), order.by=as.POSIXct(strptime("2021-11-04 05:57:00", "%Y-%m-%d %H:%M:%S")+1:10*30), born=as.POSIXct("1899-05-08"))
    
    # Round observations in z to the next hour
    index(xts3) <- as.POSIXct(trunc(index(xts3), units="mins"))
    
    # Remove duplicate times in z
    xts3_dup <- make.index.unique(xts3, drop = TRUE)
    
    xts
    2021-11-04 05:57:00 -0.19766541
    2021-11-04 05:58:00 -0.00902353
    2021-11-04 05:58:00 -2.56173420
    2021-11-04 05:59:00  0.64355622
    2021-11-04 05:59:00 -0.18794658
    2021-11-04 06:00:00  0.03005718
    2021-11-04 06:00:00  0.64367384
    2021-11-04 06:01:00  0.74716446
    2021-11-04 06:01:00 -0.29986731
    2021-11-04 06:02:00 -0.57503711
    
    > xts3_dup
                               [,1]
    2021-11-04 05:57:00 -0.19766541
    2021-11-04 05:58:00 -0.00902353
    2021-11-04 05:59:00  0.64355622
    2021-11-04 06:00:00  0.03005718
    2021-11-04 06:01:00  0.74716446
    2021-11-04 06:02:00 -0.57503711