Hi Professor:
When I read the file, the time in military time and date are stored in the same vector as follows: 2011-01-29 16:15:11.823547 I want to take all the hours saved in that file and categorize them as day and night. For example, the day would be between 06:00:00-19:00:00 and the night would be between 19:00:01-05:00:59. Given that condition, I intend to allocate time to their respective categories, and then calculate how many hours to read from the file fall into the category of the night and many more day. Then, each category be averaged, ie, the hours during the day and the hours during the night. If one day he drove 10 hours and 3 were day and 7 night, calculate the average. From: amy_ruiz_go...@hotmail.com To: rip...@stats.ox.ac.uk Subject: RE: [R] Converting the time in a numeric value Date: Sun, 10 Jul 2011 17:39:38 -0400 When I read the file, the time in military time and date are stored in the same vector as follows: 2011-01-29 16:15:11.823547 I want to take all the hours saved in that file and categorize them as day and night. For example, the day would be between 06:00:00-19:00:00 and the night would be between 19:00:01-05:00:59. Given that condition, I intend to allocate time to their respective categories, and then calculate how many hours to read from the file fall into the category of the night and many more day. Then, each category be averaged, ie, the hours during the day and the hours during the night. If one day he drove 10 hours and 3 were day and 7 night, calculate the average. > Date: Sun, 10 Jul 2011 08:15:00 +0100 > From: rip...@stats.ox.ac.uk > To: amy_ruiz_go...@hotmail.com > CC: r-help@r-project.org > Subject: Re: [R] Converting the time in a numeric value > > On Sun, 10 Jul 2011, Amy Ruiz Goyco wrote: > > > Hello: > > > I am new using R. I have a file that contain in the same columns > > date and time like for example 2011/10/03 12:34:45.123423 p.m., but > > when I read the file and display the vector, I see of this way > > "2011-10-03 12:34:45.123423". I need to convert the time in a > > numeric and the date if is possible, but I don't need this to > > compute. Thus, I used this tiempo=substr(time,12,26) to selected > > the data that I need, but I don't know how I can change this to a > > numeric values. > > You need to clarify what you mean by 'date' and 'numeric' here. At a > guess, 'numeric' might mean 'number of seconds past midnight'. We > can't even do that, since we don't know the timezone (and it differs > on DST transition days). So you need to read a well-informed article > on date-times (not the un-refereed one in R-News 4/1) to gain more > understanding. > > Also, your AM/PM indicator is very non-standard. > > But here are some pieces for you to work with > > dt <- "2011/10/03 12:34:45.123423 p.m." > t0 <- substr(dt, 12, 26) > PM <- substr(dt, 28, 31) > > date <- strptime(dt, "%Y/%m/%d") > t1 <- unclass(as.POSIXct(paste("1970-01-01", t0))) > time <- ifelse(PM == "p.m.", t1+12*3600, t1) > > > date > [1] "2011-10-03" > > print(time, digits=12) > [1] 84885.123423 > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@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. > > > > -- > Brian D. Ripley, rip...@stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 [[alternative HTML version deleted]] ______________________________________________ R-help@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.