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
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
Hi AFAIK as.Date does not accept hours. Although it is not explicitly written in help page, the name as.Date seems to me clear enough that it works only with dates. If you want to use hours, minutes ... you should use strptime for converting your values to valid date_time object. And you should also use ISOdatetime conversion function to use hours etc. in your commands. Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ogbos > Okike > Sent: Monday, April 18, 2016 3:10 PM > To: r-help at r-project.org > Subject: [R] as.Date > > 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
Date data cannot represent hour data. You need to use POSIXct or perhaps the chron class from the chron package. To use POSIXct, use ISOdatetime instead of ISOdate. Also be careful which timezone you have set as default (in most operating systems calling Sys.setenv(TZ="Etc/GMT") or similar will get you started) when you invoke ISOdatetime, since daylight savings can complicate things. Of course if daylight savings is built into your data already then you are better off choosing a timezone that understands that. See ?DateTimeClasses. -- Sent from my phone. Please excuse my brevity. On April 18, 2016 6:09:50 AM PDT, 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.[[alternative HTML version deleted]]
Dear ALL, Thank you so much for your contributions. I have made some progress. Below is a simple script I gleaned from your kind responses: Sys.setenv(TZ="Etc/GMT") dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92") times <- c("23:0:0", "22:0:0", "01:00:00", "18:0:0", "16:0:0") x <- paste(dates, times) aa<-strptime(x, "%m/%d/%y %H:%M:%S") bb<-1:5 plot(aa, bb) I tried plotting my result and I got what I am looking for. I think I am almost there. I am, however, stuck here. My data is a large file and the form differs a little from the example I used. The quotation marks in both date and time is my headache now. Such inverted commas are not in my data. I can with awk transform my data to get exactly something like dd/mm/yy. But I wont know how to make the data appear in quotation mark in R. I will once more be glad for any more help. Ogbos PS: I am still afraid of this forum. Please direct me to the right forum if this is not ok. Thanks again. On 4/18/16, peter dalgaard <pdalgd at gmail.com> wrote:> 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 > >