David Stark
2010-Jul-15 16:24 UTC
[R] How to plot a histogram of weekday frequencies in a list of dates?
Question from an [R] novice... Hi, I have a vector of date/times like the one shown below (a truncated sample of a much longer list...)> dates[1:4][1] "2006-03-16 08:41:00" "2006-03-16 10:28:00" "2006-03-16 11:03:00" [4] "2006-03-16 11:04:00" I would like to generate a weekday histogram showing the frequency of dates falling on each weekday. I know that the function weekdays() gives me the weekday for each date in my vector, but the hist() function can't plot non-numeric values. There should be a function to allow me to do this simply. Any thoughts? After having done this, I would also like to bin my histogram in 12 hr periods (Mon 7am - 7pm; Mon 7pm - Tues 7am; Tues 7am - 7pm; Tues 7pm-Wed 7am... etc) so that I can plot the frequency of event occurrences within each 12 hour period over the course of a week. Any help would be greatly appreciated. Thanks David
Wu Gong
2010-Jul-16 03:42 UTC
[R] How to plot a histogram of weekday frequencies in a list of dates?
Hope it works. ## Create a sample of dates dates <- sample(seq(ISOdate(2009,1,1), ISOdate(2009,12,31), "hour"),1000) ## Create 14 labels for barplot("Mon AM","Mon PM",etc.) labels <- format(seq(ISOdate(2010,7,11,5), ISOdate(2010,7,18,4), "12 hours"),"%a %p") ## Use table function to calculate the frequency of each time intervel ## Use format "%w %p" to get the weekday name and AM/PM indicator of the dates ## Minus 12*60*60 seconds to change AM interval from 0-12AM to 7AM-7PM barplot(table(format((dates-12*60*60), "%w %p")),names.arg=labels) ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/How-to-plot-a-histogram-of-weekday-frequencies-in-a-list-of-dates-tp2290601p2290917.html Sent from the R help mailing list archive at Nabble.com.
Wu Gong
2010-Jul-16 03:44 UTC
[R] How to plot a histogram of weekday frequencies in a list of dates?
Correction: ## Minus 7*60*60 seconds to change AM interval from 0-12AM to 7AM-7PM barplot(table(format((dates-7*60*60), "%w %p")),names.arg=labels) ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/How-to-plot-a-histogram-of-weekday-frequencies-in-a-list-of-dates-tp2290601p2290920.html Sent from the R help mailing list archive at Nabble.com.