[R] how to work with time of day (independent of date)
jim holtman
jholtman at gmail.com
Fri Oct 30 18:56:09 CET 2015
is this what you want:
> df <- structure(list(date = structure(1:8, .Label = c("2015-10-29
00:50:00",
+ "2015-10-29 09:30:00", "2015-10-29 21:10:00", "2015-10-30 00:50:00",
+ "2015-10-30 09:30:00", "2015-10-30 21:10:00", "2015-10-31 00:50:00",
+ "2015-10-31 10:30:00"), class = "factor"), value = c(88L, 17L,
+ 80L, 28L, 23L, 39L, 82L, 79L)), .Names = c("date", "value"), row.names =
c(NA,
+ -8L), class = "data.frame")
>
> # extract just the time and summarize by it
> tapply(df$value, substring(df$date, 12, 16), mean)
00:50 09:30 10:30 21:10
66.0 20.0 79.0 59.5
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Fri, Oct 30, 2015 at 1:35 PM, Daniel Nordlund <djnordlund at frontier.com>
wrote:
> I have a data frame with date/times represented as charaacter strings and
> and a value at that date/time. I want to get the mean value for each time
> of day, across days, and then plot time of day on the x-axis and means on
> the y-axis. R doesn't appear to have a built-in time of day time type
> (independent of a date), unless I have missed something. What is the best
> way to create a time variable so that I can aggregate and plot by time of
> day, with time labelled in HH:MM format. My current approach is to convert
> all date/times to the same date. I can then manage the rest of what I want
> with ggplot2. But I am wondering if there is an easier/better way to do
> deal with time of day.
>
> Here is a sample data frame.
>
> df <- structure(list(date = structure(1:8, .Label = c("2015-10-29
> 00:50:00",
> "2015-10-29 09:30:00", "2015-10-29 21:10:00", "2015-10-30 00:50:00",
> "2015-10-30 09:30:00", "2015-10-30 21:10:00", "2015-10-31 00:50:00",
> "2015-10-31 10:30:00"), class = "factor"), value = c(88L, 17L,
> 80L, 28L, 23L, 39L, 82L, 79L)), .Names = c("date", "value"), row.names =
> c(NA,
> -8L), class = "data.frame")
>
>
> Any suggestions appreciated.
>
> Dan
>
> --
> Daniel Nordlund
> Bothell, WA USA
>
> ______________________________________________
> 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]]
More information about the R-help
mailing list