Home > Software engineering >  Is there a R function to group the categorical values to type of values
Is there a R function to group the categorical values to type of values

Time:04-18

I have a data frame that includes all different types of goods, for example, apples, bananas, potatoes, tuna, salmon, oranges, and many more. All these goods are under a variable "Item". I am looking for a solution in R that can create a new variable as "Item Category" with Fruits, Vegetables, Seafood and assign all the items according to their category.

CodePudding user response:

You may prepare a list for each category and match them in a case_when statement.

library(dplyr)

df <- data.frame(item = c('apples', 'bananas', 'potatoes', 'tuna', 'salmon', 'oranges'))

df <- df %>%
  mutate(item_category = case_when(item %in% c('apples', 'bananas', 'oranges') ~ 'Fruits', 
                                  item %in% c('potatoes') ~ 'Vegetables',
                                  item %in% c('tuna', 'salmon') ~ 'SeaFood'))

df

#      item item_category
#1   apples        Fruits
#2  bananas        Fruits
#3 potatoes    Vegetables
#4     tuna       SeaFood
#5   salmon       SeaFood
#6  oranges        Fruits

CodePudding user response:

You can use fct_collapse() in forcats to collapse factor levels into manually defined groups:

library(dplyr)

# Refer to @RonakShah's example
df <- data.frame(item = c('apples', 'bananas', 'potatoes', 'tuna', 'salmon', 'oranges'))

df %>%
  mutate(
    item_category = forcats::fct_collapse(item,
      'Fruits'     = c('apples', 'bananas', 'oranges'), 
      'Vegetables' = c('potatoes'),
      'SeaFood'    = c('tuna', 'salmon'))
  )

or passing a named list to rename levels with !!!:

lev <- list('Fruits' = c('apples', 'bananas', 'oranges'), 
            'Vegetables' = c('potatoes'),
            'SeaFood' = c('tuna', 'salmon'))
df %>%
  mutate(item_category = forcats::fct_collapse(item, !!!lev))

#       item item_category
# 1   apples        Fruits
# 2  bananas        Fruits
# 3 potatoes    Vegetables
# 4     tuna       SeaFood
# 5   salmon       SeaFood
# 6  oranges        Fruits
  • Related