fct_lump_min , I can't specify the number of counts and make it OTHER.

now I'm learning forcats fct_lump_*

I can't specify the number of counts and make it OTHER.

I read

https://forcats.tidyverse.org/reference/fct_lump.html

and It said

Use fct_lump_min() to lump together all levels with fewer than `n` values

I thought,
if I use add_count + fct_lump_min, I can clasifiy "under freq = 2" .

set.seed(2021)
df <- tibble(tt = LETTERS[c(sample(26)[1:20],sample(26)[1:10],sample(26)[1:10],sample(26)[1:10])])
table(df$tt)

df %>% 
  add_count(tt) %>%
  slice(1:26) %>% 
  mutate(fff = fct_lump_min(tt,min = 2, other_level = "under 2")) %>% 
  arrange(desc(n))

return is

 1 U         4 U    
 2 I         4 I    
 3 K         4 Other
 4 U         4 U    
 5 I         4 I    
 6 S         3 S    
 7 S         3 S    
 8 V         3 Other

why V and K is include Other ?

I want to make 2 and 1 OTHER.

thank you.

Because after the slice() you only have 1 repetition of each, the order of the commands mathers.

library(tidyverse)
set.seed(2021)
df <- tibble(tt = LETTERS[c(sample(26)[1:20],sample(26)[1:10],sample(26)[1:10],sample(26)[1:10])])

df %>% 
    slice(1:26) %>% 
    add_count(tt) %>% 
    filter(tt %in% c("K", "V"))
#> # A tibble: 2 x 2
#>   tt        n
#>   <chr> <int>
#> 1 K         1
#> 2 V         1

df %>% 
    add_count(tt) %>%
    #slice(1:26) %>%
    mutate(fff = fct_lump_min(tt,min = 2, other_level = "under 2")) %>% 
    arrange(desc(n))
#> # A tibble: 50 x 3
#>    tt        n fff  
#>    <chr> <int> <fct>
#>  1 U         4 U    
#>  2 I         4 I    
#>  3 K         4 K    
#>  4 U         4 U    
#>  5 I         4 I    
#>  6 K         4 K    
#>  7 I         4 I    
#>  8 K         4 K    
#>  9 U         4 U    
#> 10 U         4 U    
#> # … with 40 more rows

Created on 2021-05-21 by the reprex package (v2.0.0)

1 Like

@andresrcs

The confirmation code, slice, was still there.
I am embarrassed.
Thanks.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.