Tidyverse data frame conversion from monthly to annual
?summarise_all See the examples.
On March 3, 2019 7:14:55 PM PST, phil at philipsmith.ca wrote:
I have a data frame in which the first column is a sequence of monthly
dates and the other columns are variables. There are a great many
variables. I want to create another data frame similar to the first
one,
but with annual values instead of monthly, created by summing the
months
within each year.
I am able to do this as shown in this reprex:
library(tidyverse)
REF_DATE <- seq(as.Date("2000/1/1"),by="month",length.out=36)
set.seed(57)
df <- data.frame(REF_DATE,
x=sample(1:100,size=36),
y=sample(1:100,size=36),
z=sample(1:100,size=36),
Year=year(REF_DATE))
df1 <- df %>%
group_by(Year) %>%
summarise(x_a=sum(x),y_a=sum(y),z_a=sum(z)) %>%
ungroup()
However, while this works for the simple case with only three
variables,
I actually have many more than three, so I am looking for a more
general
approach. I have no clue as to how to proceed. Any advice will be much
appreciated.
Philip
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Sent from my phone. Please excuse my brevity.