I know there are many versions to this question, but I am looking for a specific solution. When you have an existing character variable in a dataframe, is there an easy method for converting that variable to a factor using the tidyverse format? For example, the 2nd line of code below won't reorder the factor levels, but the last line will. How do I make the 2nd line work? There are situations when this would be useful -- importing and modifying existing datasets. Many Thanks!
df <- data.frame(x = c(1,2), y = c('post','pre')) %>%
as_factor(y, levels = c('pre','post'))
df$y <- factor(df$y, levels = c('pre', 'post'))