Search code examples
rdplyraggregate

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


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).


Solution

  • 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