1

I have a basketball data with a bunch of different types of shots, and I want to reduce the number of different names. For example, I have 'stepback jumpshot' and 'pull up jumpshot'.

I want to add a new variable that does something like:

df %>% mutate(NewVar== case when(Var1 like jumpshot then Jumpshot))

so all my different jumpshots are renamed as Jumpshot.

Edo
  • 7,567
  • 2
  • 9
  • 19
hauger2
  • 35
  • 4

2 Answers2

2

To elaborate on @r2evans comment, what you are looking for is grepl(). This function can tell you whether a string exists in another string. It will return a TRUE or FALSE. You don't actually need the mutate or the case when, and could do it with Base R:

Var1 <-  c("Free Throw", "stepback jumpshot", "pull up jumpshot", "hail mary")

df <- data.frame(Var1) 

df$Var2 <- ifelse(grepl("jumpshot", Var1, fixed = TRUE), "Jumpshot", Var1)

df

#                Var1       Var2
# 1        Free Throw Free Throw
# 2 stepback jumpshot   Jumpshot
# 3  pull up jumpshot   Jumpshot
# 4         hail mary  hail mary

But if you really want to use dplyr functions, the case statement @r2evans gave will work:


Var1 <-  c("Free Throw", "stepback jumpshot", "pull up jumpshot", "hail mary")

df <- data.frame(Var1) 

df2 <- df %>% 
  mutate(Var2 = case_when(grepl("jumpshot", Var1) ~ "Jumpshot", 
                          grepl("block", Var1) ~ "Block", 
                          TRUE ~ Var1))
df2

#                Var1       Var2
# 1        Free Throw Free Throw
# 2 stepback jumpshot   Jumpshot
# 3  pull up jumpshot   Jumpshot
# 4         hail mary  hail mary

David J. Bosak
  • 1,386
  • 12
  • 22
2

Don't forget str_detect from stringr...

Var1 <-  c("Free Throw", "stepback jumpshot", "pull up jumpshot", "hail mary")

df <- data.frame(Var1,stringsAsFactors = FALSE) 

df2 <- df %>% 
  mutate(Var2 = case_when(str_detect(Var1,"jumpshot") ~ "Jumpshot", 
                          str_detect(Var1,"block") ~ "Block", 
                          TRUE ~ Var1))

It's a little faster than grep (see What's the difference between the str_detect function in stringer and grepl and grep?)

Joe Erinjeri
  • 1,200
  • 1
  • 7
  • 15