I have an example data.frame
df
with factor and numeric data (see code below). In my real data.frame
, column B is character, so I set it to be character. How can I change column B from "character" to "factor with 26 levels" (for each unique letter)?
A=paste("a",1:200,sep="")
B=sample(letters,200,replace=T)
C=rnorm(200)
df=data.frame(A,B,C)
# Now column B is "factor with 26 levels"
df$B <- lapply(df$B, as.character)
# Now column B is "character"
# How do I convert column B back to "factor with 26 levels"?
# I tried df$B <- lapply(df$B, as.factor), but then column B is "factor with 1 levels"
I looked at posts like this: in R, how to set and retain custom levels in factor with different labels? Which work fine if you only have several levels to set, but I have 26 levels.
I assume that there must be an easier fix?