r - Using dplyr summary function on yearmon from zoo -


i have data frame values associated year , month. use yearmon class zoo package store year-month info.

my aim count average of values same year-month. however, using dplyr seems give me error.

the variable tst below reproduction

> str(tst) 'data.frame':   20 obs. of  2 variables:  $ n : int  23 24 26 27 26 23 19 19 22 22 ...  $ ym:class 'yearmon'  num [1:20] 2004 2004 2004 2004 2004 ... > dput(tst) structure(list(n = c(23l, 24l, 26l, 27l, 26l, 23l, 19l, 19l,  22l, 22l, 22l, 22l, 26l, 26l, 19l, 22l, 26l, 25l, 22l, 18l),      ym = structure(c(2004, 2004, 2004, 2004, 2004.08333333333,      2004.08333333333, 2004.08333333333, 2004.08333333333, 2004.08333333333,      2004.16666666667, 2004.16666666667, 2004.16666666667, 2004.16666666667,      2004.25, 2004.25, 2004.25, 2004.25, 2004.33333333333, 2004.33333333333,      2004.33333333333), class = "yearmon")), .names = c("n", "ym" ), row.names = c(na, 20l), class = "data.frame") 

and error was

> tst %>% group_by(ym) %>% summarize(ave=mean(n)) error: column 'ym' has unsupported type : yearmon 

is there way make work both zoo , dplyr, or i'll have encode year-month separately?

as error says, class not supported in dplyr. can change ym to class dplyr supports , work

library(dplyr) tst %>%         group_by(ym = as.numeric(ym)) %>%        summarise(ave = mean(n)) #        ym      ave #1 2004.000 25.00000 #2 2004.083 21.80000 #3 2004.167 23.00000 #4 2004.250 23.25000 #5 2004.333 21.66667 

or @g.grothendieck mentioned in comments, can replace group_by group_by(ym = as.date(ym) or group_by(ym = format(ym, "%y-%m"))


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -