Home > other >  Create a new variable conditioned to an external list
Create a new variable conditioned to an external list

Time:10-21

I am having some trouble creating a new variable in R in a dataframe conditioned to an external list of values:

# Sample dataframe 

employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
color <- c('blue', `green`, `red`)

data.frame(employee, salary, color)

#List of colors and categories 
color_categories <- list( cold = c("blue", "green"), warm = c("red", "orange"))

I want to create a new variable in the dataframe with the color category depending on the color chosen by each employee, so I want to get something like this result:

    employee  salary   color  category
1   John Doe  21000    blue   cold
2 Peter Gynn  23400    green  cold
3 Jolie Hope  26800    red    warm

Thank you very much!!!

CodePudding user response:

Here's a solution using tidyverse:

-Use bind_rows on your list to create a data.frame, then pivot_longer so we can join on colors.Then rename name as category (result of pivot_longer)

library(tidyverse)

data.frame(employee, salary, color) %>% 
  left_join(bind_rows(color_categories) %>% pivot_longer(everything()), by = c("color" = "value")) %>% 
  rename(category = name)

This gives us:

    employee salary color category
1   John Doe  21000  blue     cold
2 Peter Gynn  23400 green     cold
3 Jolie Hope  26800   red     warm

CodePudding user response:

Cannot say this is more tidy than package dependent solutions, but this is a solution in base:

color_categories <- data.frame(unlist(color_categories))
colnames(color_categories) <- "color"
color_categories$category <- gsub("[0-9]", "", rownames(color_categories))

merge(df, color_categories, by = "color") # df being the sample dataframe

CodePudding user response:

Using ifelse

 df$category = ifelse(df$color == 'blue' | df$color == 'green' , 'cold', NA)
 df$category = ifelse(df$color == 'red' | df$color == 'orange' , 'warm', df$category)
  •  Tags:  
  • r
  • Related