[R] Creating xts objects from csv file

Joshua Ulrich jo@h@m@u|r|ch @end|ng |rom gm@||@com
Tue Jul 21 19:56:55 CEST 2020


Please reply on-list, so others may benefit from the conversation.

I'm glad you got it working, despite not being sure what the problem
was.  I agree that you should use POSIXct instead of POSIXlt.  POSIXlt
are fairly large, since they're a list of 9 elements each with an
observation for each timestamp.

Here's a sample of your file (as text) and a way you can read it into
R using read.csv.zoo().  Replace the `irreg` object with your filename
(e.g. `"irreg.csv"`).

Lines <- "DTG,Seq
1/5/2010 2:30,1
1/5/2010 10:32,2
1/5/2010 12:03,3
1/6/2010 6:01,4
1/6/2010 17:55,5
1/6/2010 23:42,6
1/7/2010 1:14,7
1/7/2010 5:00,8
1/7/2010 9:40,9
1/7/2010 13:14,10
1/7/2010 13:22,11
1/7/2010 16:12,12
1/7/2010 20:10,13
1/7/2010 21:52,14
1/7/2010 22:22,15"
irreg <- textConnection(Lines)
z <- zoo::read.csv.zoo(irreg, format = "%m/%d/%Y %H:%M", tz = "", drop = FALSE)
x <- xts::as.xts(z)  # if you want an xts object


On Tue, Jul 21, 2020 at 12:09 PM Jeff Reichman <reichmanj using sbcglobal.net> wrote:
>
>        Joshua
>
>        I tried POSIXct and it worked just fine???
>
>        Then I tried modifying my code to
>
>        myDat$DTG <- as.POSIXlt(myDat$DTG, format = "%m/%d/%Y %H:%M")
>        irreg <- xts(x=myDat[,-1],order.by= as.POSIXct(myDat$DTG))
>
>        which seemed to work, so I'm not sure what I was doing wrong. But I also gather I should probably use POSIXct objects  as opposed to POSIXlt objects.
>
>        Jeff
>
>
> -----Original Message-----
> From: Joshua Ulrich <josh.m.ulrich using gmail.com>
> Sent: Tuesday, July 21, 2020 9:28 AM
> To: reichmanj using sbcglobal.net
> Cc: R-Help <r-help using r-project.org>
> Subject: Re: [R] Creating xts objects from csv file
>
> Your example works for me. Can you provide a sample of the CSV you're trying to read, and the commands you use to read it?  The output of
> sessionInfo() might also be helpful.
>
>
> On Mon, Jul 20, 2020 at 3:42 PM Jeff Reichman <reichmanj using sbcglobal.net> wrote:
> >
> > R-Help Forum
> >
> >
> >
> > Starting to work with xts objects but can't figure out what I'm doing wrong
> > when converting *.csv file with a dtg variable to a *.xts object. When I'm
> > converting to an appropriate time object all I get are NA, so that's my
> > first issue.
> >
> >
> >
> > dtg <- c("1/5/2010 2:30", "1/5/2010 10:32", "1/5/2010 12:03")
> >
> > seq <- c(1,2,3)
> >
> >
> >
> > dat <- data.frame(dtg, seq)
> >
> >
> >
> > dat$dtg <- as.POSIXlt(dat$dtg, format = "%m/%d/%Y %H:%M")
> >
> >
> >
> > dat.xts <- xts(x = dat[,-1], order.by = dat[,1])
> >
> >
> >
> > head(dat.xts)
> >
> >
> >
> > Sincerely
> >
> >
> >
> > Jeff Reichman
> >
> >
> >         [[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



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com



More information about the R-help mailing list