R语言 根据列在一年中的月份计算列的总和

e7arh2l6  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(191)

如何按月份对数据库进行分组,并获得每个列的分组值?
下面是我的 Dataframe 图片:

我试着这样做,但没有效果:

df_publications <- df_publications %>% group_by(publication_date) 
                   %>% count()
wko9yo5t

wko9yo5t1#

Dplyr summarize across everything方法:

df<-data.frame(publication_date=c("2015 Jul","2015 Jul","2015 Aug","2015 Aug"),
               Asym=c(3,5,1,2),
               Auth=c(5,7,2,3),
               Cert=c(1,2,3,4))

library(tidyverse)

df %>% 
  group_by(publication_date) %>% 
  summarize(across(everything(), sum))

#  publication_date  Asym  Auth  Cert
#1 2015 Aug             3     5     7
#2 2015 Jul             8    12     3

base::xtabs()方法,需要命名所有数据行:

xtabs(cbind(Auth, Asym, Cert)~., data=df)
#publication_date Auth Asym Cert
#        2015 Aug    5    3    7
#        2015 Jul   12    8    3

感谢@akrun https://stackoverflow.com/a/74619313/10276092的替代方案

xtabs(sprintf("cbind(%s)~.", toString(names(df)[-1])), data = df)
eit6fx6z

eit6fx6z2#

SQL中,您可以使用子查询:

SELECT Year_, Month_, SUM(Counts)
FROM (
        SELECT YEAR(DATEADD(MM,DATEDIFF(MM,0,StartTime),0))'Year_'
              ,DATENAME(MONTH,DATEADD(MM,DATEDIFF(MM,0,StartTime),0))'Month_'
              ,TestName
              ,CASE WHEN Testname = 'POE Business Rules' THEN (count(TestName)*36) 
                    WHEN TestName = 'Submit' THEN (COUNT(TestName)*6) 
                    ELSE 0 
               END 'Counts'
        FROM VExecutionGlobalHistory
        GROUP BY YEAR(DATEADD(MM,DATEDIFF(MM,0,StartTime),0))
                ,DATENAME(MONTH,DATEADD(MM,DATEDIFF(MM,0,StartTime),0))
                ,TestName
        )sub
GROUP BY Year_, Month_
ORDER BY CAST(CAST(Year_ AS CHAR(4)) + Month_ + '01' AS DATETIME)

更新:添加了ORDER BY,以按年份/月份排序,最早的排在最前面。

相关问题