Re: [R] Creating xts objects from csv file
Hello, I've cc-ed the list. Yes, I believe that generally speaking, POSIXct is better than POSIXlt. POSIXlt is a complicated structure, POSXct is much simpler and gives less problems. Datetimes are a problem because they are datetimes but POSIXct is a good way of trying to make things simpler. It's also the class returned by packages like lubridate, functions ymd_hms and similar. SO I think it's the prefered class for problems with datetime variables. Rui Barradas Às 17:59 de 21/07/2020, Jeff Reichman escreveu: Rui So generally would it be better to us POSIXct than POSIXlt? Jeff -Original Message- From: Rui Barradas Sent: Tuesday, July 21, 2020 5:58 AM To: reichm...@sbcglobal.net Subject: Re: [R] Creating xts objects from csv file Hello, Why it didn't work I don't know but the opposite happens to me, I rarely use as.POSIXlt. This is because "POSIXlt" objects are complicated, list-like objects. Try ct <- as.POSIXct("2020-07-21 12:30") lt <- as.POSIXlt("2020-07-21 12:30") attributes(ct) unclass(ct) attributes(lt) unclass(lt) POSIXlt objects can be usefull when their components are needed but many times the problems are simple and the POSIXct class is simpler. Rui Barradas Às 00:40 de 21/07/2020, Jeff Reichman escreveu: Rui Yes that worked. I rarely if ever use the POSIXct function . Wonder why POSIXct worded and POSIXlt didn't JEff -Original Message- From: Rui Barradas Sent: Monday, July 20, 2020 4:37 PM To: reichm...@sbcglobal.net; r-help@r-project.org Subject: Re: [R] Creating xts objects from csv file Hello, I cannot reproduce the error, your code runs as expected. Try as.POSIXct? Hope this helps, Rui Barradas Às 21:32 de 20/07/2020, Jeff Reichman escreveu: 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@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. -- Este e-mail foi verificado em termos de vírus pelo software antivírus Avast. https://www.avast.com/antivirus __ R-help@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.
Re: [R] Creating xts objects from csv file
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 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 > Sent: Tuesday, July 21, 2020 9:28 AM > To: reichm...@sbcglobal.net > Cc: R-Help > 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 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@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 __ R-help@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.
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 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@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 __ R-help@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.
Re: [R] Creating xts objects from csv file
Are you sure you loaded the xts library? On Mon, 20 Jul 2020 at 16:32, Jeff Reichman 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) > > > [[alternative HTML version deleted]] > > __ > R-help@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. > -- John Kane Kingston ON Canada [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Creating xts objects from csv file
Might this be related to the stringsAsfactors change? -- the new default is FALSE, the old was TRUE. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Jul 20, 2020 at 2:37 PM Rui Barradas wrote: > Hello, > > I cannot reproduce the error, your code runs as expected. Try as.POSIXct? > > Hope this helps, > > Rui Barradas > > Às 21:32 de 20/07/2020, Jeff Reichman escreveu: > > 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@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. > > > -- > Este e-mail foi verificado em termos de vírus pelo software antivírus > Avast. > https://www.avast.com/antivirus > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Creating xts objects from csv file
Hello, I cannot reproduce the error, your code runs as expected. Try as.POSIXct? Hope this helps, Rui Barradas Às 21:32 de 20/07/2020, Jeff Reichman escreveu: 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@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. -- Este e-mail foi verificado em termos de vírus pelo software antivírus Avast. https://www.avast.com/antivirus __ R-help@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.