Rodrigo
Rodrigo

Reputation: 231

How to split data monthly in R

I have a dataset corresponding to the register of buys in a store, it is something like this:

Date          BuyId      Price     Description    Category
2010-01-01    101028        100    ...            ...
2010-01-01    101028        100    ...            ...
2010-01-01    101028        100    ...            ...
2010-01-01    101028        100    ...            ...
...

The dates in the dataframe goes from 2010-01-10 to 2015-04-01 and I would like to split it monthly so I can plot the volume of buys per month in each year, I mean something like:

Date        Count
2010-Jan    19128
2010-Feb    1232
...
...
2015-Mar    28363
2015-Apr    12834

I've been having a hard time with this specially because I'm pretty new to R and I don't know so many functions.

I tried to split the data using split but I couldn't make it. Does anyone have a clue how can I do this?

Upvotes: 2

Views: 2461

Answers (1)

m0nhawk
m0nhawk

Reputation: 24168

You can use dplyr for this:

df %>%
  mutate(new.date = cut.Date(as.Date(Date, format = '%Y-%m-%d'), "month")) %>%
  group_by(new.date) %>%
  summarise(count = n())

mutate will create a new column with cutted dates, group_by by month and summarise will count the number of entries.


Also, if you need year and abbreviation month, just add one more mutate:

 %>%
mutate(new.date = format(as.Date(new.date), "%Y-%b"))

Upvotes: 6

Related Questions