I have a factor and want to make it a column in a data frame. But I was surprised to find out that it was automatically turned into a character, even if I specified stringsAsFactors.
Here is the MWE:
a <- data.frame(dummy=1:5, stringsAsFactors = TRUE)
b <- as.factor(c("Monday", "Tuesday", "Monday", "Thursday", "Tuesday"))
a["d"] <- b
> levels(a["d"])
NULL
How do I do the assignment so I get an actual factor, keeping the original levels?
It is important that I cannot use solutions which convert the factor afterwards, because in the example, it would get the levels 'Monday Thursday Tuesday' while I have prepared a factor which has all proper levels, and in the needed sequence (in this example, it would be all the days of the week in row).
It is because of the difference in extracting the columns. The
a['d']
is still adata.frame
with 'd' as column, whilea[, 'd']
ora[['d']]
ora$d
all extracts the 'd' column as avector
withclass
asfactor
. To see the difference, we check thestr()