group categories according to whether they meet a sequence of conditions with tidyverse

Viewed 55

I have a problem on how to recategorize a variable according to whether it meets a certain condition or not. That is, if the category does not meet the criteria, it is assigned to another category that does.

My data has the following form:

data = data.frame(firm_size = c("Micro", "Small", "Medium","Big"), 
                  employees = c(5,10,100,1000))

> data 
     firm_size employees
1     Micro         5
2     Small        10
3    Medium       100
4       Big      1000

So, if my condition is that I must group the companies that have less than 10 employees and then combine them with the other category that does meet the criteria

> new_data 
     firm_size    employees
1    Micro-Small     15
3    Medium          100
4    Big             1000

What I'm trying to do is write a function that generalizes this procedure, for example, that also works if my data is

> data 
     firm_size employees
1     Micro       5
2     Small       8
3    Medium       9
4       Big      1000


> new_data 
     firm_size            employees
1    Micro-Small-Medium   22
4    Big                  1000

I think that this can be done with the tools of the tidyverse.

Thanks in advance

3 Answers

Here's an approach with tally:

library(dplyr)
size <- 10

data %>% 
  arrange(firm_size,desc(employees)) %>%
  group_by(firm_size = c(as.character(firm_size[employees > size]),
                         rep(paste(firm_size[employees <= size], collapse = "-"),
                             sum(employees <= size)))) %>%
  tally(employees, name = "employees")
## A tibble: 3 x 2
#  firm_size   employees
#  <chr>           <dbl>
#1 Big              1000
#2 Medium            100
#3 Small-Micro        15

And for your second set of data:

data2 %>% 
  arrange(firm_size,desc(employees)) %>%
  group_by(firm_size = c(as.character(firm_size[employees > size]),
                         rep(paste(firm_size[employees <= size], collapse = "-"),
                             sum(employees <= size)))) %>%
  tally(employees, name = "employees")
## A tibble: 2 x 2
#  firm_size          employees
#  <chr>                  <int>
#1 Big                     1000
#2 Medium-Small-Micro        22

Data

data <- structure(list(firm_size = structure(c(3L, 4L, 2L, 1L), .Label = c("Big", 
"Medium", "Micro", "Small"), class = "factor"), employees = c(5, 
10, 100, 1000)), class = "data.frame", row.names = c(NA, -4L))


data2 <- structure(list(firm_size = structure(c(3L, 4L, 2L, 1L), .Label = c("Big", 
"Medium", "Micro", "Small"), class = "factor"), employees = c(5L, 
8L, 9L, 1000L)), class = "data.frame", row.names = c("1", "2", 
"3", "4"))

You can use the great forcats package

library(tidyverse)

data <- data.frame(
  firm_size = c("Micro", "Small", "Medium", "Big", "Small"),
  employees = c(5, 10, 100, 1000, 10)
)


# If you need n groups


data %>%
  mutate(firm_size2 = firm_size %>% as_factor() %>% fct_lump(n = 2, w = employees)) %>% 
  group_by(firm_size2) %>% 
  summarise(sum_emp = sum(employees),.groups = "drop")
#> # A tibble: 3 x 2
#>   firm_size2 sum_emp
#>   <fct>        <dbl>
#> 1 Medium         100
#> 2 Big           1000
#> 3 Other           25


# If you need at least x on the sum of a vector

data %>%
  mutate(firm_size2 = firm_size %>% as_factor() %>% fct_lump_min(min = 10, w = employees)) %>% 
  group_by(firm_size2) %>% 
  summarise(sum_emp = sum(employees),.groups = "drop")
#> # A tibble: 4 x 2
#>   firm_size2 sum_emp
#>   <fct>        <dbl>
#> 1 Small           20
#> 2 Medium         100
#> 3 Big           1000
#> 4 Other            5

Created on 2020-06-11 by the reprex package (v0.3.0)

Yet another solution, set into a custom function:

library(tidyverse)

mymerge <- function(dat, min) {
  merged_dat <- dat %>% 
    filter(if_else(employees <= min, TRUE, FALSE)) %>% 
    summarize(firm_size = str_flatten(firm_size, collapse = " - "),
              employees = sum(employees))

  dat %>% 
    filter(if_else(employees <= min, FALSE, TRUE)) %>% 
    bind_rows(merged_dat)
}

mymerge(data, 30)
      firm_size employees
1        Medium       100
2           Big      1000
3 Micro - Small        15

mymerge(data, 300)
               firm_size employees
1                    Big      1000
2 Micro - Small - Medium       115
Related