我知道我可以使用sapply
来查找数据列的类,如下所示:
sapply(data, class)
上面的代码类似于:How do I get the classes of all columns in a data frame?
但是,如何确定和存储fac
类的列名factor
的向量?
答案 0 :(得分:1)
fac <- names(data)[sapply(data, is.factor)]
答案 1 :(得分:0)
另一种类似的方法是使用purrr
软件包:
names(iris)[purrr::map_lgl(iris, is.factor)]
#"Species"
which(map_lgl(iris, is.factor) == TRUE) #this will return the column index as well.
#Species
#5