Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading