An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110710/94e5c452/attachment.pl>
Converting the time in a numeric value
2 messages · Amy Ruiz Goyco, jim holtman
try this:
x <- data.frame(time = seq(as.POSIXct("2011-1-1 00:00"), by = '1 hour', length = 300))
# default to night
x$day <- FALSE
# now set days to TRUE
# get hour/minutes
temp <- format(x$time, "%H%M")
x$day[temp >= "0600" & temp < "1900"] <- TRUE
head(x, 30)
time day 1 2011-01-01 00:00:00 FALSE 2 2011-01-01 01:00:00 FALSE 3 2011-01-01 02:00:00 FALSE 4 2011-01-01 03:00:00 FALSE 5 2011-01-01 04:00:00 FALSE 6 2011-01-01 05:00:00 FALSE 7 2011-01-01 06:00:00 TRUE 8 2011-01-01 07:00:00 TRUE 9 2011-01-01 08:00:00 TRUE 10 2011-01-01 09:00:00 TRUE 11 2011-01-01 10:00:00 TRUE 12 2011-01-01 11:00:00 TRUE 13 2011-01-01 12:00:00 TRUE 14 2011-01-01 13:00:00 TRUE 15 2011-01-01 14:00:00 TRUE 16 2011-01-01 15:00:00 TRUE 17 2011-01-01 16:00:00 TRUE 18 2011-01-01 17:00:00 TRUE 19 2011-01-01 18:00:00 TRUE 20 2011-01-01 19:00:00 FALSE 21 2011-01-01 20:00:00 FALSE 22 2011-01-01 21:00:00 FALSE 23 2011-01-01 22:00:00 FALSE On Sun, Jul 10, 2011 at 5:49 PM, Amy Ruiz Goyco
<amy_ruiz_goyco at hotmail.com> wrote:
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_goyco at hotmail.com To: ripley at 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: ripley at stats.ox.ac.uk To: amy_ruiz_goyco at hotmail.com CC: r-help at 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 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.
-- Brian D. Ripley, ? ? ? ? ? ? ? ? ?ripley at 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 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve?