I have a df that looks like:
# Groups: telefono1 [360]
nombre telefono1 telefono2 ads_ao ads_am ads_mas ads_ma total_price_ao total_price_am total_price_mas~ total_price_ma
<chr> <chr> <lgl> <chr> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
1 CRIST~ 60***0400 NA 0 0 0 0 0 0 0 0
2 CRIST~ 60***0400 NA 0 61 0 0 0 0 0 0
3 CRIST~ 60***0400 NA 0 0 0 0 0 0 0 0
4 DIEGO~ 60***0844 NA 0 0 0 0 0 0 0 0
5 Diego 60***0844 FALSE 0 0 0 2 0 0 0 22500
6 TALLE~ 60***4848 NA 2 0 0 0 59000 0 0 0
7 TALLE~ 60***4848 NA 0 311 0 0 0 0 0 0
8 Rub?n 60***4848 FALSE 0 0 0 2 0 0 0 59000
I need to group by telefono1 and then keep the entire row which value across columns "ads_ao, ads,am, ads_mas and ads_ma" was the maximum value.
In the example, the right solution were rows #2, #5 and #7 because they are the ones, groupping by telefono1, that have the maximum value across mentioned columns.
I tried it with dplyr
group_by(telefono1) %>%
mutate(max_value = max(ads_ao, ads_am, ads_mas, ads_ma)) %>%
ungroup()
But with this method I get a new variable with max value in all rows and then I can't use a top_n() to get the row with the max value because all are equal.
Please, anyone know any solution? Thanks!!