0

I saw a lot of similar questions but none of the answers have worked for me... Here's my example data

company<-c("c1","c1","c2","c2","c2","c3","c3","c3","c4","c4", "c4")
subsegment<- c("Sub2","Sub3", "Sub1","Sub2", "Sub3", "Sub1","Sub2", "Sub3", "Sub1","Sub2", "Sub3")
values<-c(120,300,30,300,1800,10,96,277, 10, 400, 1100)
df<- data.frame(company,subsegment, values)

company.keys<-c("c1","c2","c3","c4")
company.description <- c("CocaCola", "Pepsi", "Boing", "Perrier")
lookup.company<- as.data.frame(company.keys, company.description)

subseg.keys<- c("Sub1", "Sub2", "Sub3")
subseg.description<- c("SoftDrink", "Snacks", "Other")
lookup.subseg<- as.data.frame(subseg.keys, subseg.description)

I have my original data frame df I want to take the column 'company' and go search in 'lookup.company' to replace the keys with the description. I know how to do it like this but I want a way that will use my lookup data frames:

 df$subsegment[df$subsegment == "Sub1"] <- "SoftDrink"

I tried but I get an error:

df %>%
mutate_at(subsegment, funs(ifelse(. %in% lookup.subseg$subseg.keys, lookup.subseg$subseg.description[match(., lookup.subseg$subseg.keys)], .)))

Error: Warning message: funs() was deprecated in dplyr 0.8.0. Please use a list of either functions or lambdas:

As I said, I know it has been asked before but none of the answers I saw seemed to work for me I would like to use dplyr for the solution

1 Answer 1

1
library(tidyverse)

df %>% 
  left_join(rownames_to_column(lookup.subseg), c("subsegment" = "subseg.keys")) %>% 
  select(-subsegment, subsegment = rowname)

   company values subsegment
1       c1    120     Snacks
2       c1    300      Other
3       c2     30  SoftDrink
4       c2    300     Snacks
5       c2   1800      Other
6       c3     10  SoftDrink
7       c3     96     Snacks
8       c3    277      Other
9       c4     10  SoftDrink
10      c4    400     Snacks
11      c4   1100      Other
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.