0
votes

I have taken the daily differences in my data set for example stored in diff.vec.

Regardless of year, I need to have all days + associated diff column, sorted by month.

All days for jan 2000, jan 2001, jan 2002 etc etc... will all be grouped into month 1.... then again all days in feb 1928, feb 1929, 1930 will all be grouped in month 2 so on and so forth... and it will also pull the corresponding diff column with it also...

An example data is below:

# dummy data
diff.vec <- c(NA, -0.04, -0.17,  0.11,  0.02, -0.18, -0.13, -0.02,  0.12)
date.vec <- c("1928-01-03", "1928-01-04", "1928-02-05", "1928-02-06", "1928-03-07","1928-03-09", "1928-03-10", "1928-01-11", "1928-01-12")

df <- data.frame(date.vec,diff.vec) # make data frame from vectors
library(lubridate)
df$date.vec <- ymd(df$date.vec)  # Convert Date Column [1] to Date format 

My date format is yyyy-mm-dd.

I have tried a few things with dplyr:

# Group each daily difference by month
group <- df %>%
  dplyr::mutate(day = format(Date, "%d"), month = format(Date, "%m"), year = format(Date, "%y")) %>%
  group_by(day, month,year)

It is somewhat close.

1

1 Answers

2
votes

super easy my little friend

df %>% mutate(mymonth = lubridate::month(mydate)) %>% group_by(mymonth)