Creating 2 classes from numeric data

Hi, I'm fairly new to R, and to coding in general, and there are two quite basic things I cannot figure out how to do, any help on either would be much appreciated;

  1. In my dataset I have a numeric column DXCURREN, which contains the values 1, 2 or 3 for each line, I want to separate this data into two classes, HC=1, and non-HC=2 or 3

  2. one column of data contains years, but the data is in the format '/YYYY', is there a way to automatically remove or ignore the '/' at the beginning of the data?

Take a look at this

library("tidyverse")
set.seed(643046)
d <- tibble(DXCURREN = sample(x = 1:3, size = 20, replace = TRUE),
            YEAR = str_c("/", sample(x = 1940:2020, size = 20, replace = TRUE)))

d %>% 
  mutate(CLASS = case_when(DXCURREN == 1 ~ "HC",
                           DXCURREN == 2 ~ "non-HC",
                           DXCURREN == 3 ~ "non-HC"),
         YEAR = str_replace(string = YEAR,
                            pattern = "/",
                            replacement = ""))

Hope it helps! :slightly_smiling_face:

1 Like

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

Thanks a million managed to get this to work

Good to hear, if my answer solves your problem, please mark it as solution :+1: