I have a data-frame of dates (Date object); see bottom. I'm trying to convert them to day-of-week and then draw a histogram, but ideally where the labels are 'Monday'...'Sunday' (not numeric)
I have two distinct problems:
- It's easy to convert a Date object to day-of-week, but the result is string or numeric, not an object.
- When I get a histogram, the bins and labels are wrong (see below).
If I use weekdays(dat)
, the output is string ("Monday"...) which cannot be used in hist()
.
Alternatively, if I convert to numeric data, how to get string labels on hist()
?
> dotw <- with( month.day.year(dat[,1]), day.of.week(month,day,year) )
> hist(xxx,labels=c('M','Tu','W','Th','F','Sa','Su'),col='black') # WTF?!
> hist(dotw,xlab=list('M','Tu','W','Th','F','Sa','Su'))
Does not work as intended for labeling. What's with the 0.5-width bins? And also, how to prevent the lack of gap between Sunday->0 and Monday->1? Ideally, no gaps between columns.
My data looks like:
> dat
[1] "2010-04-02" "2010-04-06" "2010-04-09" "2010-04-10" "2010-04-14" "2010-04-15" "2010-04-19"
[8] "2010-04-21" "2010-04-22" "2010-04-23" "2010-04-26" "2010-04-28" "2010-04-29" "2010-04-30"
...
> str(dat)
Date[1:146], format: "2010-04-02" "2010-04-06" "2010-04-09" "2010-04-10" "2010-04-14" "2010-04-15" ...
> str(weekdays(dat))
chr [1:146] "Friday" "Tuesday" "Friday" "Saturday" "Wednesday" "Thursday" "Monday" ...
> hist(weekdays(dat))
Error in hist.default(weekdays(dat)) : 'x' must be numeric