Raghuraman Ramachandran
2012-Apr-22 16:53 UTC
[R] difficulty in Formatting time series data
Dear R-Gurus I have a data frame (from CSV file) which has its first column called Date. The Date is in the format mm/dd/yyyy. I was trying to get the weekday for these dates and I tried using wday() and day.of.week() functions and both of them gave me precisely the wrong answers. I think the issue lies in the proper formatting of dates. The class of this column is a factor class and hence I tried converting into POSIXlt, xts, zoo objects and yet I could not get the weekday correctly. Anyone has any suggestions please? Many thanks Raghu [[alternative HTML version deleted]]
Yes.... dput() for a reproducible example with some minimal reproducible code (and the packages "day.of.week" and wday() come from...) x <- xts(10, Sys.Date()) wday(x) seems fine for me. "precisely the wrong answers" -- interesting turn of phrase. Michael On Sun, Apr 22, 2012 at 12:53 PM, Raghuraman Ramachandran <optionsraghu at gmail.com> wrote:> Dear R-Gurus > > I have a data frame (from CSV file) which has its first column called Date. > The Date is in the format mm/dd/yyyy. I was trying to get the weekday for > these dates and I tried using wday() and day.of.week() functions and both > of them gave me precisely the wrong answers. I think the issue lies in the > proper formatting of dates. The class of this column is a factor class and > hence I tried converting into POSIXlt, xts, zoo objects and yet I could not > get the weekday correctly. Anyone has any suggestions please? > > Many thanks > Raghu > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Raghu, On 22 April 2012 09:53, Raghuraman Ramachandran <optionsraghu@gmail.com>wrote:> I have a data frame (from CSV file) which has its first column called Date. > The Date is in the format mm/dd/yyyy. I was trying to get the weekday for > these dates and I tried using wday() and day.of.week() functions and both > of them gave me precisely the wrong answers. I think the issue lies in the > proper formatting of dates. The class of this column is a factor class and > hence I tried converting into POSIXlt, xts, zoo objects and yet I could not > get the weekday correctly. Anyone has any suggestions please? >Try this: # assume dataIn is where the CSV files data is... dataIn$Date <- as.POSIXct(dataIn$Date, format='%m/%d/%y') dataIn <- cbind(dataIn, day.of.week = format(dataIn$Date, format='%A') -- Sent from my mobile device Envoyait de mon portable [[alternative HTML version deleted]]