Cristina
Cristina

Reputation: 23

How to use slice to get the biggest values just from a variable?

I'm having serious problems by using the function dplyr :: slice().

I need to create with mutate() a new variable that shows the biggest values just for a variable and an observation. Specifically, I need to show the winner party in an election in each town, but I always get the biggest winner of all the dataframe instead of the winner of each town.

My teacher has told me to use: slice_max(my_variable, n = 1). But I need to link it with another variable. Any ideas?

votos_cyl %>% 
  filter(prov %in% c("Ãvila")) %>%
  mutate(winner = slice_max(votos_partido_pc, n = 1)) %>%
  distinct(mun, .keep_all= TRUE) %>% 
  select(mun, part, ganador) %>% 
  arrange(desc(part)) %>% 
  slice_max(part, n=10) 

When I code this, it shows error due to numeric variable. Before, I used function max() but the result was the biggest observation of my df as I said before

Upvotes: 1

Views: 1090

Answers (1)

jpiversen
jpiversen

Reputation: 3212

The problem

slice_max() is meant for dataframes, not vectors (which is what you are supplying it inside mutate().

library(dplyr)

# Slice max works with dataframes
mtcars %>% 
  slice_max(hp)
#>               mpg cyl disp  hp drat   wt qsec vs am gear carb
#> Maserati Bora  15   8  301 335 3.54 3.57 14.6  0  1    5    8

# But gives error inside mutate
mtcars %>% 
  mutate(
    x = slice_max(hp)
  )
#> Error in `mutate()`:
#> ! Problem while computing `x = slice_max(hp)`.
#> Caused by error in `UseMethod()`:
#> ! no applicable method for 'slice_max' applied to an object of class "c('double', 'numeric')"

Solution

In your case, you can use which.max() to get the party with the most votes:

# Let's pretend this is your data
df <- 
  mtcars %>% 
  tibble::rownames_to_column("party") %>% 
  select(
    party,
    town = cyl,
    votes = hp
  ) %>% 
  head()

df
#>               party town votes
#> 1         Mazda RX4    6   110
#> 2     Mazda RX4 Wag    6   110
#> 3        Datsun 710    4    93
#> 4    Hornet 4 Drive    6   110
#> 5 Hornet Sportabout    8   175
#> 6           Valiant    6   105

# To get the party with the most votes, you can use which.max()
df %>% 
  group_by(town) %>% 
  mutate(
    winner = party[which.max(votes)]
  )
#> # A tibble: 6 × 4
#> # Groups:   town [3]
#>   party              town votes winner           
#>   <chr>             <dbl> <dbl> <chr>            
#> 1 Mazda RX4             6   110 Mazda RX4        
#> 2 Mazda RX4 Wag         6   110 Mazda RX4        
#> 3 Datsun 710            4    93 Datsun 710       
#> 4 Hornet 4 Drive        6   110 Mazda RX4        
#> 5 Hornet Sportabout     8   175 Hornet Sportabout
#> 6 Valiant               6   105 Mazda RX4

Created on 2022-03-30 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions