data.table factors and labels

Hello! I try to label some of my data.table factors. Earlier with data frames factor, levels and and then labels worked, but now all rows turn to NA's with real data. What could be the cause? Besides if you have advanced advice for the job with data.tables because the real data has many columns to factorize and label. I have been checking the forcats package, but did not understand how to label levels. Maybe I am just tired, it should not be this difficult :no_mouth:. Levels could be 1 = "Male", 2 = "Female"

DT_MY = data.table(
  q126 = c(200, 201, 202, 203),
  q127 = c(1, 2, 1, 2),
  q128 = c(1, 5, 2, 4),
  q139 = c(0.8, 3.0, 10.0, NA),
  q131 = c(55, 56, 80, 79),
  q132 = c(1.62, 1.74, 1.69, 1.71),
  q133 = c(1, 2, 1, NA),
  q134 = c(1, 5, 4, 2))
DT_MY
DT_MY[, q127 := as.factor(q127)]
levels(DT_MY$q127) 
DT_MY[,"q127"] <- factor(DT_MY[,"q127"],
	 levels = c(1,2),
	 labels = c("Male","Female"))

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