Creating a string variable based on another variable

Hello all,

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

Thanks!

Dan

df <- data.frame(stringsAsFactors = FALSE,
                 Var1 = c("apples", "cats", "green apples"))
df$Var2 <- ifelse(grepl("apples", df$Var1), "Fruit", NA)
df
#>           Var1  Var2
#> 1       apples Fruit
#> 2         cats  <NA>
#> 3 green apples Fruit

Created on 2019-03-28 by the reprex package (v0.2.1)

Thank you for all of your help!

Dan

Quick follow up. What if you have multiple clauses? it seems the "NA" aspect overrides all other clauses

Var 1 Var 2
apples fruit
cats animal
green cats animal
green apples fruit
red cats animal

In this case

If it contains apples, its fruit if it contains cats it’s an animal.

Thanks

What about this?

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

Created on 2019-03-29 by the reprex package (v0.2.1)

You can also use if - else if - else

Got it! thanks again for your help!

Dan

Your code works perfectly, but I can't translate it. I'm new to R

df$Var2="NA"
mutate(df$var2 = case_when(grepl("apples", df$var1) ~ "fruit",
grepl("cats", df$var1) ~ "animal"))

Error: unexpected '=' in "mutate(df$var2 ="

Error: unexpected ')' in " grepl("cats", df$var1) ~ "animals")"

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.

Thanks again for your help!

Dan

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.