Search code examples
rdataframedplyrtidyversedummy-variable

How do convert a categorical variable into multiple dummy variables in R?


Here I have a dataset with a column name as Age = (24 or under, 25 to 34, 35 to 44, 45 to 54, 25 to 34, 24 or under,35 to 44, 25 to 34, 45 to 54)

Now I need to convert the values for the categorical variable "Age" as follows: 24 or under equal to 1, 25 to 34 equal to 2, 35 to 44 equal to 3, 45 to 54 equal to 4

Can anyone help me here?

Many thanks in advance.


Solution

  • You can use nested ifelse statements:

    set.seed(12)
    df <- data.frame(Age = c(sample(c("24 or under", "25 to 34", "35 to 44", "45 to 54"), 20, replace = T)))
    df$Age_new <- ifelse(df$Age == "24 or under", 1,
                         ifelse(df$Age == "25 to 34", 2,
                                ifelse(df$Age == "35 to 44", 3, 4)))
    

    Result:

    df
               Age Age_new
    1     25 to 34       2
    2     35 to 44       3
    3  24 or under       1
    4     45 to 54       4
    5  24 or under       1
    6     35 to 44       3
    7     45 to 54       4
    8     25 to 34       2
    9     45 to 54       4
    10    35 to 44       3
    11 24 or under       1
    12    35 to 44       3
    13    25 to 34       2
    14 24 or under       1
    15    25 to 34       2
    16    35 to 44       3
    17    25 to 34       2
    18    25 to 34       2
    19    35 to 44       3
    20    25 to 34       2