I have a simple question, but I can't find the correct code to do it. I tried grepl, but that didn't work. I want to create a value (string) for a new variable based on another variable containing a specific word.
If Var1 contains "apples" then var2="fruit"
Sample dataset
Var1 Var 2
apples Fruit
cats NA
green apples Fruit
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df <- data.frame(stringsAsFactors = FALSE,
Var1 = c("apples", "cats", "green cats", "green apples", "red cats"))
df %>%
mutate(Var2 = case_when(grepl("apples", Var1) ~ "fruit",
grepl("cats", Var1) ~ "animal"))
#> Var1 Var2
#> 1 apples fruit
#> 2 cats animal
#> 3 green cats animal
#> 4 green apples fruit
#> 5 red cats animal
If you don't want to use the pipe operator %>% then you have to specify the .data parameter for mutate() and you shouldn't use df$Var1 when working with dplyr just use Var1
mutate(.data = df, Var2 = case_when(grepl("apples", Var1) ~ "fruit",
grepl("cats", Var1) ~ "animal"))
#> Var1 Var2
#> 1 apples fruit
#> 2 cats animal
#> 3 green cats animal
#> 4 green apples fruit
#> 5 red cats animal
If you want to learn how to use dplyr an the rest of the tidyverse tools you can read this free online book.