phii m@iii@g oii phiiipsmith@c@
2019-Mar-04 03:14 UTC
[R] Tidyverse data frame conversion from monthly to annual
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
Jeff Newmiller
2019-Mar-04 03:21 UTC
[R] 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.
Amit Mittal
2019-Mar-04 03:24 UTC
[R] Tidyverse data frame conversion from monthly to annual
Try using time series objects in xts. Should be easy. At worst once the xts object is automatically arranged by date you have to note the row numbers for each year period and I think data would be ok to handle unless it is more than 30-40 years when you would look up other xts options. Xts objects would automatically take the date column and use them as row ids so there is obviously another simple loop you can generate for summing up . There would also be easy functions once you start with xts. Best Regards Amit +91 7899381263 Please request Skype as available? 5th Year FPM (Ph.D.) in Finance and Accounting Area Indian Institute of Management, Lucknow, (U.P.) 226013 India http://bit.ly/2A2PhD AEA Job profile : http://bit.ly/AEAamit FMA 2 page profile : http://bit.ly/FMApdf2p SSRN top10% downloaded since July 2017:?http://ssrn.com/author=2665511 From: phil at philipsmith.ca Sent: 04 March 2019 08:45 To: r-help at r-project.org Subject: [R] Tidyverse data frame conversion from monthly to annual 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. [[alternative HTML version deleted]]
phii m@iii@g oii phiiipsmith@c@
2019-Mar-04 03:38 UTC
[R] Tidyverse data frame conversion from monthly to annual
summarise_all() does the trick. Thanks very much for the help. Philip On 2019-03-03 22:14, 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
PIKAL Petr
2019-Mar-04 07:23 UTC
[R] Tidyverse data frame conversion from monthly to annual
Hi aggregate is another option. aggregate(df[, 2:4], list(df$Year), sum) Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of > phil at philipsmith.ca > Sent: Monday, March 4, 2019 4:15 AM > To: r-help at r-project.org > Subject: [R] Tidyverse data frame conversion from monthly to annual > > 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.Osobn? ?daje: Informace o zpracov?n? a ochran? osobn?ch ?daj? obchodn?ch partner? PRECHEZA a.s. jsou zve?ejn?ny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner?s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/ D?v?rnost: Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a podl?haj? tomuto pr?vn? z?vazn?mu prohl??en? o vylou?en? odpov?dnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/