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 members,
> I have:
>
>
> > head(testOHLC)
> 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:
>
>
> > head(testOHLC["2007-01-09"])
> 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:
>
> > head(testOHLC[-"2007-01-09"])
> 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
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
--
Joshua Ulrich | about.me/joshuaulrich
FOSS Trading | www.fosstrading.com