[R] removing dates from xts object..

akshay kulkarni @k@h@y_e4 @end|ng |rom hotm@||@com
Mon Jul 25 22:56:17 CEST 2022


Dear Joshua,
                     Thanks a lot...

Yours sincrely,
AKSHAY M KULKARNI
________________________________
From: Joshua Ulrich <josh.m.ulrich using gmail.com>
Sent: Tuesday, July 26, 2022 2:20 AM
To: akshay kulkarni <akshay_e4 using hotmail.com>
Cc: R help Mailing list <r-help using r-project.org>
Subject: Re: [R] removing dates from xts object..

On Mon, Jul 25, 2022 at 3:34 PM akshay kulkarni <akshay_e4 using 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.
>

'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)]

> Thanking you,
> Yours sincrely,
> AKSHAY M KULKARNI
>
> ________________________________
> From: Joshua Ulrich <josh.m.ulrich using gmail.com>
> Sent: Tuesday, July 26, 2022 1:56 AM
> To: akshay kulkarni <akshay_e4 using hotmail.com>
> Cc: R help Mailing list <r-help using r-project.org>
> Subject: Re: [R] removing dates from xts object..
>
> 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 using 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 using 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<http://www.fosstrading.com>



--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com<http://www.fosstrading.com>

	[[alternative HTML version deleted]]



More information about the R-help mailing list