3

I have data with a Date. I want to group all things by the month interval in which they reside.

So say I have:

date, value
2015-04-01, 1
2015-04-28, 2
2015-05-04, 3
2015-05-09, 4

Then I would like to end up with the groupings

[1]
2015-04-01, 1
2015-04-28, 2

[2]
2015-05-04, 3
2015-05-09, 4

Note all the data is retained. It's just grouped based on the month.

Thanks in advance

perrpell
  • 89
  • 1
  • 4
  • possible duplicate of [R language: how to split a data frame](http://stackoverflow.com/questions/3302356/r-language-how-to-split-a-data-frame) – zx8754 Apr 27 '15 at 07:46
  • I was also considering to mark it as a duplicate, but decided not to. I found many questions on splitting data frames or [working with months and dates](http://stackoverflow.com/questions/9749598/r-obtaining-month-and-year-from-a-date) but none that combines the two, so I thought the question was motivated. – Backlin Apr 27 '15 at 14:39

2 Answers2

6
str <- "date, value
2015-04-01, 1
2015-04-28, 2
2015-05-04, 3
2015-05-09, 4"

tab <- read.csv(textConnection(str), colClasses=c("POSIXct", "integer"))
split(tab, format(tab$date, "%Y-%m"))

$`2015-04`
        date value
1 2015-04-01     1
2 2015-04-28     2

$`2015-05`
        date value
3 2015-05-04     3
4 2015-05-09     4
Backlin
  • 14,612
  • 2
  • 49
  • 81
  • how can I extract the Maximum value of each split tab ? for example : 2015-04 max value : 2 @Backlin – Ben2pop Sep 27 '16 at 15:34
  • @Ben2pop `sapply(split_tab, function(x) max(x$value))`. Personally I find it easier to work with data frames rather than lists, so I'd use the _dplyr_ package and call `tab %>% group_by(month=format(tab$date, "%Y-%m")) %>% summarise(max_value=max(value))`. – Backlin Oct 12 '16 at 12:45
0

Do you want this because you want to be able to filter easily by month?

It might be easier if you create a new column "month". You could then filter by month.

Floni
  • 475
  • 2
  • 13
  • Just for others looking for a solution if you simply create a column months using the function `months()` this can lead to problems when using the function `split()` namely that it would not split the months in order. – Nick Jul 09 '20 at 14:38