0

I am trying to familiarize myself with the different functions and R and ran into some difficulties recreating the following code using the replace() instead of case_when().

The packages I am using are:

library(tidyverse)
library(nycflights13)

The following is the code I am trying to recreate

diamonds %>%
  select(carat)%>%
  mutate(new_carat = case_when(carat <.28 ~ "too small",
                               carat>=.7 & carat<=1.04~"too big",
                               carat>4 ~ NA_character_,
                               TRUE~ as.character(carat)))

The following code is what I have tried

diamonds%>%
  select(carat)%>%
  mutate(carat_new = replace(carat,carat<=.28, "too small"),
         carat_new = replace(carat, carat>=.7 & carat <=1.04, "too big"),
         carat_new= replace(carat, carat>4, NA_character_),
         carat_new= replace(carat, TRUE, as.character(carat)))

This didn't change anything except converting the data from dbl to chr

0 Answers0