rvariablesfactorslevels

how to correct a mistake made in the levels of a factor variable?


let's say I have this dataframe

d = data.frame(x = c("1","1 2", "1 3", "2 3", "3", "4"))
d

and it has the variable x as a factor

d$x = as.factor(d$x)

However I discover an error in three of the levels that I wrote.

So I want to replace the values of these variables and their levels as follows :

I want to replace 1 2 with 1

I want to replace 1 3 with 1

I want to replace 2 3 with 2

levels(d$x)

so I want to correct it. when using the following method :

d$x[which(d$x == "1 2")] <- "1"
d$x[which(d$x == "1 3")] <- "1"
d$x[which(d$x == "2 3")] <- "2"

It create levels as follows

1 1 1 2 3 4

What I wish is the levels as follows

1 2 3 4

What should I do to handle this problem ? thanks


Solution

  • You can use fct_collapse:

    library(dplyr)
    library(forcats)
    d %>% 
      mutate(x = fct_collapse(x, 
                              "1" = c("1", "1 2", "1 3"),
                              "2" = c("2", "2 3")))
      x
    1 1
    2 1
    3 1
    4 2
    5 3
    6 4