Hi All-- I am relatively new to R. I am reading a csv file via read.table (MyFile). The data types in the file are date, string, integer, and time. I was able to read all the data and manipulated correctly except time, e.g., 12:30. I used as.Date to convert date and string and integer were easily done. I could not figure out how to convert the time data correctly. I tried chron but w/o success and I read that POSIXlt and POSIXct work only for date and time (e.g. 01/02/1999, 12:30:20). I did not try the lubridate package. Is there a way to read time data without date attached to it like mine? I am grateful for any help and thanks in advance?EKE Here is an example of my data when read into R via read.table AA Date Name T1 T2 N1 1 312171 7/1/1995 OF 13:37 1:43 123 [[alternative HTML version deleted]]
Hi EKE, Your problem may be that the date strings are being read as a factor. Try using stringsAsFactors=FALSE when you read the data in. Another way is to convert your dates to strings when passing to as.Date: as.Date(as.character(mydf$Date),"%m/%d/%Y") Jim On Sun, Jun 5, 2016 at 10:53 PM, Ek Esawi <esawiek at gmail.com> wrote:> Hi All-- > > > > I am relatively new to R. I am reading a csv file via read.table (MyFile). > The data types in the file are date, string, integer, and time. I was able > to read all the data and manipulated correctly except time, e.g., 12:30. I > used as.Date to convert date and string and integer were easily done. I > could not figure out how to convert the time data correctly. I tried chron > but w/o success and I read that POSIXlt and POSIXct work only for date and > time (e.g. 01/02/1999, 12:30:20). I did not try the lubridate package. Is > there a way to read time data without date attached to it like mine? > > > > I am grateful for any help and thanks in advance?EKE > > > > Here is an example of my data when read into R via read.table > > > > AA Date Name T1 T2 > N1 > > 1 312171 7/1/1995 OF 13:37 1:43 123 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Hi> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ek Esawi > Sent: Sunday, June 5, 2016 2:54 PM > To: r-help at r-project.org > Subject: [R] Reading and converting time data via read.table > > Hi All-- > > > > I am relatively new to R. I am reading a csv file via read.table (MyFile). > The data types in the file are date, string, integer, and time. I was able to > read all the data and manipulated correctly except time, e.g., 12:30. I used > as.Date to convert date and string and integer were easily done. I could not > figure out how to convert the time data correctly. I tried chron but w/o > success and I read that POSIXlt and POSIXct work only for date and time (e.g.If you used strptime for converting your datae/time value you can tune it by format to be able to read differently formated data.> strptime("01/02/1999, 12:30:20", format="%d/%m/%Y, %H:%M:%S")[1] "1999-02-01 12:30:20 CET"> strptime("01/02/1999, 12:30", format="%d/%m/%Y, %H:%M")[1] "1999-02-01 12:30:00 CET"> strptime("01/02/1999, 12", format="%d/%m/%Y, %H")[1] "1999-02-01 12:00:00 CET">> str(strptime("01/02/1999, 12", format="%d/%m/%Y, %H"))POSIXlt[1:1], format: "1999-02-01 12:00:00">Regards Petr> 01/02/1999, 12:30:20). I did not try the lubridate package. Is there a way to > read time data without date attached to it like mine? > > > > I am grateful for any help and thanks in advance?EKE > > > > Here is an example of my data when read into R via read.table > > > > AA Date Name T1 T2 > N1 > > 1 312171 7/1/1995 OF 13:37 1:43 123 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
As far as I know, base R does not have a class for storing times that are not associated with a date, and recognizing that they are times. That being the case, I don't think there is a way to convert them to some sort of time class while reading them into R using read.table(). I would read them into R as character strings, and then convert them (it would take only a few extra lines of code). How you convert them depends on the next question, which is: What do you need to do with those times? For example, are T1 and T2 the times associated with two events that both occurred on the specified Date? If that is the case, I would probably form two POSIXct variables by combining the Date with T1 and the Date with T2. Or, do you just need to be able to sort your data by T1, or by T2? Or do you need to calculate the time differences (such as T2-T1) to get the number of minutes between those two times? -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 6/5/16, 5:53 AM, "R-help on behalf of Ek Esawi" <r-help-bounces at r-project.org on behalf of esawiek at gmail.com> wrote:>Hi All-- > > > >I am relatively new to R. I am reading a csv file via read.table (MyFile). >The data types in the file are date, string, integer, and time. I was able >to read all the data and manipulated correctly except time, e.g., 12:30. I >used as.Date to convert date and string and integer were easily done. I >could not figure out how to convert the time data correctly. I tried chron >but w/o success and I read that POSIXlt and POSIXct work only for date and >time (e.g. 01/02/1999, 12:30:20). I did not try the lubridate package. Is >there a way to read time data without date attached to it like mine? > > > >I am grateful for any help and thanks in advance?EKE > > > >Here is an example of my data when read into R via read.table > > > > AA Date Name T1 T2 >N1 > >1 312171 7/1/1995 OF 13:37 1:43 123 > > [[alternative HTML version deleted]] > >______________________________________________ >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.
The canonical way to store times is as difftime vectors. However, there is no simple way to import e.g. HH:MM data directly into such vectors, so you need to embed such times into a longer string that includes a fixed date. After conversion to POSIXct you can subtract the fixed date to get the difftime values. It is also possible to let the conversion to POSIXct pick "today" by default, but you can sometimes get into trouble if you subtract out "today" on a different day, so I wouldn't recommend it. You also may encounter difficulty with daylight savings in this process... but that usually requires knowing a bit more about your data, and if you are already working with time only data you may not be able to fix such problems anyway. -- Sent from my phone. Please excuse my brevity. On June 8, 2016 10:20:38 AM PDT, "MacQueen, Don" <macqueen1 at llnl.gov> wrote:>As far as I know, base R does not have a class for storing times that >are >not associated with a date, and recognizing that they are times. That >being the case, I don't think there is a way to convert them to some >sort >of time class while reading them into R using read.table(). I would >read >them into R as character strings, and then convert them (it would take >only a few extra lines of code). How you convert them depends on the >next >question, which is: > >What do you need to do with those times? > >For example, are T1 and T2 the times associated with two events that >both >occurred on the specified Date? If that is the case, I would probably >form >two POSIXct variables by combining the Date with T1 and the Date with >T2. >Or, do you just need to be able to sort your data by T1, or by T2? Or >do >you need to calculate the time differences (such as T2-T1) to get the >number of minutes between those two times? > >-Don > >-- >Don MacQueen > >Lawrence Livermore National Laboratory >7000 East Ave., L-627 >Livermore, CA 94550 >925-423-1062 > > > > > >On 6/5/16, 5:53 AM, "R-help on behalf of Ek Esawi" ><r-help-bounces at r-project.org on behalf of esawiek at gmail.com> wrote: > >>Hi All-- >> >> >> >>I am relatively new to R. I am reading a csv file via read.table >(MyFile). >>The data types in the file are date, string, integer, and time. I was >able >>to read all the data and manipulated correctly except time, e.g., >12:30. I >>used as.Date to convert date and string and integer were easily done. >I >>could not figure out how to convert the time data correctly. I tried >chron >>but w/o success and I read that POSIXlt and POSIXct work only for date >and >>time (e.g. 01/02/1999, 12:30:20). I did not try the lubridate package. >Is >>there a way to read time data without date attached to it like mine? >> >> >> >>I am grateful for any help and thanks in advance?EKE >> >> >> >>Here is an example of my data when read into R via read.table >> >> >> >> AA Date Name T1 T2 >>N1 >> >>1 312171 7/1/1995 OF 13:37 1:43 >123 >> >> [[alternative HTML version deleted]] >> >>______________________________________________ >>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. > >______________________________________________ >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]]