I want to aggregate a dataframe in R by week and I am trying to use lubridate to do it.
date = as.Date(c('2006-05-02','2007-05-03','2006-05-04','2006-05-05','2006-05-08','2006-05-09'))
total = c(1,2,3,4,5,10)
df=data.frame(date, total)
I used the lubridate packages to do the following;
df$wk = weeks(agg$date)
agg = aggregate(data=agg, total ~ date + variable , FUN=sum)
This does not seem to return anything that works. You can cast the weeks to strings, but then you would need to cast the weeks back to normal R dates.
df$wk = as.character(weeks(agg$date))
agg = aggregate(data=agg, total ~ date , FUN=sum)
This poses another problem, the dates are now strings that look like this;
"113029d 0H 0M 0S"
I want to use ggplot
on the agg
dataframe, so I would need to convert this string into something that ggplot can understand. as.Date()
obviously doesn't work and it seems like I might be able to convert the days into a unix_timestamp but that seems like I am doing too much effort.
How do I convert lubridates into normal R dates such that I can perform the aggregation? Normal R dates work perfectly fine in the aggregate
function so I think I would prefer to only use lubridate for the binning of dates into weeks.
agg
, what isvalue
andvariable
? – David Arenburgweeks
does not work onDate
object , rather on an numeric value, so your method won't work. Please just show us the desired output and I think it will be easily solved using base R – David Arenburg