Lets say I have a data frame as follows:
gageID date flow_cms
1011000 1937-02-19 25.768334
1011000 1937-02-20 24.918828
1011000 1937-02-21 24.069322
I want to aggregate the rows that have the same month summing the flow, and store the result into a new data value: the first day of every month; in order to obtain the following output:
gageID date sum_monthly_flow
1011000 1937-02-01 500.2222
1011000 1937-03-01 589.222
I'm using this line:
>rowsum(qfile$flow_cms, format(qfile$date, '%Y-%m-01'))
and I obtain the right sum, but I want also to reduce the record days in a unique day: the first of every month! with the strip shows above, R cannot recognize the left coloumn as data (or date).
Help would be very much appreciated!
Another solution:
Using
data.table
andlubridate
you can try:Note that it is assumed that
date
is already of classDate
and thatgageID
is another grouping parameter.Alternatively, the call to a function from another package can be avoided using
data.table
's ownmday()
function (thanks to @Henrik):Data
Here, Abdou's sample data is used:
First make sure your "date" column is properly formatted as a date object in R:
Then we can use
format
to extract the month and year, andgroup_by
that for a sum and take the first date:This will give you the first taken record for each month in the data.