[R] as.Date

peter dalgaard pdalgd at gmail.com
Mon Apr 18 15:54:51 CEST 2016


The most important thing is that Date objects by definition do not include time of day. You want to look at ISOdatetime() and as.POSIXct() instead. And beware daylight savings time issues.

-pd

On 18 Apr 2016, at 15:09 , Ogbos Okike <giftedlife2014 at gmail.com> wrote:

> Dear All,
> 
> I have a data set containing year, month, day and counts as shown below:
> data <- read.table("data.txt", col.names = c("year", "month", "day", "counts"))
> Using the formula below, I converted the data to as date and plotted.
> 
> new.century <- data$year < 70
> 
> data$year <- ifelse(new.century, data$year + 2000, data$year + 1900)
> 
> data$date <- as.Date(ISOdate(data$year, data$month, data$day))
> 
> The form of the data is:
> 16 1 19 9078
> 16 1 20 9060
> 16 1 21 9090
> 16 1 22 9080
> 16 1 23 9121
> 16 1 24 9199
> 16 1 25 9289
> 16 1 26 9285
> 16 1 27 9245
> 16 1 28 9223
> 16 1 29 9298
> 16 1 30 9327
> 16 1 31 9365
> 
> Now, I wish to include time (hour) in my data. The new data is of the form:
> 05 01 06 14    3849
> 05 01 06 15    3845
> 05 01 06 16    3836
> 05 01 06 17    3847
> 05 01 06 18    3850
> 05 01 06 19    3872
> 05 01 06 20    3849
> 05 01 06 21    3860
> 05 01 06 22    3868
> 05 01 06 23    3853
> 05 01 07 00    3839
> 05 01 07 01    3842
> 05 01 07 02    3843
> 05 01 07 03    3865
> 05 01 07 04    3879
> 05 01 07 05    3876
> 05 01 07 06    3867
> 05 01 07 07    3887
> 
> I now read the data as:
> data <- read.table("data.txt", col.names = c("year", "month", "day",
> "counts", "hour")) and also included hour in data$date <-
> as.Date(ISOdate(data$year, data$month, data$day))
> i.e data$date <- as.Date(ISOdate(data$year, data$month, data$day, data$hour)).
> 
> However, these did not work.
> 
> Can you please assist be on how to get this date and time in the right
> format. The right format I got without hour looks like : 2005-12-29"
> "2005-12-29" "2005-12-29" "2005-12-29" "2005-12-29"
> [8696] "2005-12-29" "2005-12-29" "2005-12-29" "2005-12-29" "2005-12-29"
> [8701] "2005-12-29" "2005-12-29" "2005-12-29" "2005-12-29" "2005-12-29"
> [8706] "2005-12-29" "2005-12-29" "2005-12-29" "2005-12-29" "2005-12-29"
> 
> I used this in my plot. Please I want this format to include hour.
> 
> Many thanks for your help. I am just a newbe. I am not sure if this
> forum is the right one. After registration, I tried to post to Nabble
> forum where I registered but could not succeed.
> 
> If there is a mistake, please help/direct me to the right forum.
> 
> Best regards
> Ogbos
> 
> ______________________________________________
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list