如何使用两个或更多行的聚合来创建新行?

huangapple go评论59阅读模式
英文:

How to create a new row using aggregation of two or more rows?

问题

我在RStudio上运行。因此,我运行一个相当大的数据库,并运行一个代码来基于我想要的列获得一个新的数据框 (`Results_Final_Status`)。

```R
Results_Final_Status = Results_Final %>%
  group_by(status) %>%
  dplyr::summarise(total=n()) %>%
  arrange(-total)
状态 总计
完成 7083
+1 圈 3850
引擎故障 2011
+2 圈 1593
事故 1044

因此,我的目标是创建一个新的数据框,并聚合“+1 圈”和“+2 圈”,创建一个新值“其他”,并将这两个值的总和添加到总计列中。

我尝试过使用 mutate 函数和 case_when 函数,但似乎缺少了一些东西,我在RStudio中一直出现错误。我期望得到一个新的数据框,其中这些值在某种程度上被聚合,以便创建一个新表,这样我的可视化更容易观察(否则我会有大约100行具有不同值和许多“总计”值)。


<details>
<summary>英文:</summary>

I&#39;m running on RStudio. So, I run a pretty big database, and run a code to get a new dataframe (`Results_Final_Status`) based on the columns that I want.

Results_Final_Status = Results_Final %>%
group_by(status) %>%
dplyr::summarise(total=n()) %>%
arrange(-total)`


|status      | total  |
|------------|--------|
|Finished    |  7083  |
|+1 Lap      |  3850  |
|Engine      |  2011  |
|+2 Laps     |  1593  |
|Accident    | 1044   |


So my objective is to create a new dataframe, and aggregate &quot;+1 Lap&quot; and &quot;+2 Lap&quot; creating a new value &quot;OTHER&quot; and the total SUM of this two values is added in the total column.

I&#39;ve tried the `mutate` function and the `case_when` function, but something is missing and I keep having errors on RStudio. I&#39;m expecting a new dataframe, with this values somewhat aggregated in order to create a new table, so my visualization is easier to observe (otherwise I would have like 100 rows with different values and a lot of &quot;total&quot; values).

</details>


# 答案1
**得分**: 0

也许这是一种蛮力方法,但您可以将行附加到您当前的数据框:

```R
library(tidyverse)

Results_final <- tibble(status=c('Finished','+1 Lap','Engine', '+2 Laps','Accident'),
                        total=c(7083,3850,2011,1593,1044)
                        )

Results_final_status <- Results_final %>%
  add_row(.data = data.frame(status='Other',
                             total=sum(Results_final$total[Results_final$status=='+1 Lap'|
                                                             Results_final$status=='+2 Laps'])))

Results_final_status
英文:

Perhaps a brute force method but you can append rows to your current dataframe:

library(tidyverse)

Results_final &lt;- tibble(status=c(&#39;Finished&#39;,&#39;+1 Lap&#39;,&#39;Engine&#39;, &#39;+2 Laps&#39;,&#39;Accident&#39;),
                        total=c(7083,3850,2011,1593,1044)
                        )

Results_final_status &lt;- Results_final %&gt;% 
  add_row(.data = data.frame(status=&#39;Other&#39;,
                             total=sum(Results_final$total[Results_final$status==&#39;+1 Lap&#39;|
                                                             Results_final$status==&#39;+2 Laps&#39;])))

Results_final_status

huangapple
  • 本文由 发表于 2023年7月13日 18:51:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定