Define new variable in R

I am using 'diamonds' dataset from 'tidyverse' package. My task is to define a new variable 'color2' with 2 levels 'good' and 'bad' of which 'good' corresponds the best 3 levels of 'color' and 'bad' corresponds the rest 4 levels. Here is my code:

diamonds %>% mutate(color2 = ifelse(levels(diamonds$color)[4],"bad","good"))

However, NA is returned. Could someone tell me what went wrong with my code? Thank you so much!

The first argument to ifelse() needs to be a logical--something that evaluates to TRUE or FALSE.

1 Like

Using if_else instead of ifelse and with the arguments correctly assigned:

library(tidyverse)

diamonds %>%
  mutate(
    color2 = if_else(
      condition = color == levels(diamonds$color)[4],
      true = "bad",
      false = "good"
    )
  )
#> # A tibble: 53,940 x 11
#>    carat cut       color clarity depth table price     x     y     z color2
#>    <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr> 
#>  1  0.23 Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43 good  
#>  2  0.21 Premium   E     SI1      59.8    61   326  3.89  3.84  2.31 good  
#>  3  0.23 Good      E     VS1      56.9    65   327  4.05  4.07  2.31 good  
#>  4  0.29 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63 good  
#>  5  0.31 Good      J     SI2      63.3    58   335  4.34  4.35  2.75 good  
#>  6  0.24 Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48 good  
#>  7  0.24 Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47 good  
#>  8  0.26 Very Good H     SI1      61.9    55   337  4.07  4.11  2.53 good  
#>  9  0.22 Fair      E     VS2      65.1    61   337  3.87  3.78  2.49 good  
#> 10  0.23 Very Good H     VS1      59.4    61   338  4     4.05  2.39 good  
#> # ... with 53,930 more rows

Created on 2022-06-08 by the reprex package (v2.0.1)

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.