Your data is a tbl_df
. I don't have your data, but we can look at an example using mtcars
.
library(dplyr)
tbl_df(mtcars)[, "mpg"]
# Source: local data frame [32 x 1]
#
# mpg
# (dbl)
# 1 21.0
# 2 21.0
# 3 22.8
# 4 21.4
# 5 18.7
# 6 18.1
# 7 14.3
# 8 24.4
# 9 22.8
# 10 19.2
# .. ...
It's still a data frame, whereas in base R it would have been dropped to an atomic vector. dplyr:::`[.tbl_df`
does not drop single columns, as is done in [.data.frame
from base R. This is why we can't run factor()
on it.
factor(tbl_df(mtcars)[, "mpg"])
# Error in sort.list(y) : 'x' must be atomic for 'sort.list'
# Have you called 'sort' on a list?
So you'll need to use [[
, as in df[["my_col"]]
, or just use $
.
df[["my_col"]] <- factor(df[["my_col"]])
Note: When you use the $
operator you can do it without the quotes around the column name.
df$my_col <- factor(df$my_col)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…