0

I have a data frame with a column of characters. I am trying to change this column to a ordered factors, but everytime I try to use factor(), the entire column is transformed into NAs. However, if I delete the "labels" parameter, it works just fine. I wanted to understand why I am getting this wrong.

I tried using:

data[3] <- factor(data[3], levels = c(1:4), labels = c("Ensino médio completo", "Ensino superior incompleto","Ensino superior completo","Pós-graduado"), ordered = T)

but this just makes all the characters strings be transformed into NAs. Although, trying the following "worked":

data[3] <- factor(data[3], levels = c("Ensino médio completo", "Ensino superior incompleto", "Ensino superior completo","Pós-graduado"), ordered = T)
  • https://stackoverflow.com/help/minimal-reproducible-example – deschen Mar 19 '23 at 07:05
  • 1
    Because `levels` and `labels` are different things. `levels` are what is currently present in your vector while `labels` are what you want it to look like in the end. ie if you want the current value `"Ensino médio completo"` to be a `1` then you use `labels` Not the other way round. Also just to point out, you should consider using `data[[3]]` or `data[,3]` to get a vector and work on it instead of `data[3]` which will return a list – Onyambu Mar 19 '23 at 07:20

0 Answers0