按组汇总所有列

编程入门 行业动态 更新时间:2024-10-27 14:21:14
本文介绍了按组汇总所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很肯定这是一个非常简单的答案,但我似乎无法在多种条件下进行汇总或投射

I'm positive that this is an incredibly easy answer but I can't seem to get my head around aggregating or casting with Multiple conditions

我有一张桌子看起来像这样:

I have a table that looks like this:

> head(df, n=10L) STATE EVTYPE FATALITIES INJURIES 1 AL TORNADO 0 15 3 AL TORNADO 0 2 4 AL TORNADO 0 2 5 AL TORNADO 0 2 6 AL TORNADO 0 6 7 AL TORNADO 0 1 9 AL TORNADO 1 14 11 AL TORNADO 0 3 12 AL TORNADO 0 3 13 AL TORNADO 1 26

显然,这种情况还在继续……我要做的就是崩溃按照STATE和EVTYPE的方法,我正在总结死亡和伤害,因此,如果这10行是我的有效数据集,那么结果将是一个单行数据帧:

Obviously this goes on... What I want to do is to collapse by STATE and EVTYPE Summing Fatalities and Injuries as I go so if these 10 rows were my ful dataset the result would be a single row data frame of:

STATE EVTYPE FATALITIES INJURIES 1 AL TORNADO 2 74

我的完整画面有很多州和很多EVTYPES

My Complete frame has many States and many EVTYPES

推荐答案

您可以尝试

library(dplyr) df %>% group_by(STATE, EVTYPE) %>% summarise_each(funs(sum))

aggregate(.~STATE+EVTYPE, df, sum)

更多推荐

按组汇总所有列

本文发布于:2023-11-22 04:13:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!