Skip to content

removing dates from xts object..

5 messages · akshay kulkarni, Joshua Ulrich

#
Dear members,
                         I have:
HINDUNILVR.NS.Open HINDUNILVR.NS.High HINDUNILVR.NS.Low HINDUNILVR.NS.Close
2007-01-02             217.80             219.00            215.45              216.40
2007-01-03             217.00             217.65            211.05              212.00
2007-01-04             213.00             214.25            209.65              210.60
2007-01-05             211.40             214.25            209.55              213.35
2007-01-08             213.35             213.35            207.10              210.10
2007-01-09             210.10             214.20            208.70              209.85
           HINDUNILVR.NS.Volume HINDUNILVR.NS.Adjusted
2007-01-02              2626898               155.0396
2007-01-03              4603921               151.8872
2007-01-04              5486460               150.8841
2007-01-05              5706066               152.8545
2007-01-08              3760443               150.5260
2007-01-09              5474633               150.3468

AND:
HINDUNILVR.NS.Open HINDUNILVR.NS.High HINDUNILVR.NS.Low HINDUNILVR.NS.Close
2007-01-09              210.1              214.2             208.7              209.85
           HINDUNILVR.NS.Volume HINDUNILVR.NS.Adjusted
2007-01-09              5474633               150.3468

BUT:
Error in -"2007-01-09" : invalid argument to unary operator

How do remove rows with certain dates in an xts object? This doesn't work:

x[-dates] (however, this does: x[dates])

Many thanks in advance......

Yours sincreley
AKSHAY M KULKARNI
#
This is the most straightforward and general way I can think of quickly:

library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix, dateFormat = "Date")
head(x)
##                Open     High      Low    Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185

y <- x[!(index(x) %in% as.Date(c("2007-01-04", "2007-01-05")))]
head(y)
##                Open     High      Low    Close
## 2007-01-02 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767
## 2007-01-06 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185
## 2007-01-08 50.03555 50.10363 49.96971 49.98806
## 2007-01-09 49.99489 49.99489 49.80454 49.91333

Best,
Josh
On Mon, Jul 25, 2022 at 3:20 PM akshay kulkarni <akshay_e4 at hotmail.com> wrote:

  
    
#
Dear joshua,
                      THanks for the reply

If I have a list of dates, it should be like this right?

y <- x[!(index(x) %in% dates)]
 where dates is a character vector of dates.

Thanking you,
Yours sincrely,
AKSHAY M KULKARNI
#
On Mon, Jul 25, 2022 at 3:34 PM akshay kulkarni <akshay_e4 at hotmail.com> wrote:
'dates' needs to be a vector of Date-classed values, not character

# character vector
dates_chr <- c("2007-01-04", "2007-01-05")
# Date vector -- use this below
dates_Date <- as.Date(dates_chr)

y <- x[!(index(x) %in% dates_Date)]

  
    
#
Dear Joshua,
                     Thanks a lot...

Yours sincrely,
AKSHAY M KULKARNI