英文:
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'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 "+1 Lap" and "+2 Lap" creating a new value "OTHER" and the total SUM of this two values is added in the total column.
I've tried the `mutate` function and the `case_when` function, but something is missing and I keep having errors on RStudio. I'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 "total" 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 <- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论