使用dplyr中的mutate_each将所有数值变量转换为factor

时间:2015-12-06 21:47:25

标签: r dplyr

我试图使用dplyr中的mutate_each来汇总因子中数据集的数值变量。

library(dplyr)
data(iris)
tbl_df(iris) ->iris

# I can transform all variables in factor
iris %>% mutate_each(funs(as.factor)) %>% summary
# I can transform some variables in factor
iris %>% mutate_each(funs(as.factor),one_of("Sepal.Length", "Petal.Length")) %>% summary

但我的目标是将所有数字变量转换为因子,所以我试试这个:

iris %>% mutate_each(funs(as.factor),sapply(iris,is.numeric)) %>% summary # should be a good way, but it doesn't

另一次尝试

iris %>% mutate_each(funs(as.factor),one_of(names(iris)[sapply(iris,is.numeric)]))
# Error in one_of(vars, ...) : object 'iris' not found

iris %>% mutate_each(funs(as.factor),names(iris)[sapply(iris,is.numeric)])
#Error in one_of(vars, ...) : object 'iris' not found

# anyway the one_of function dont seems to work in mutate_each
vars<-names(iris)[sapply(iris,is.numeric)]
iris %>%   mutate_each_(funs(as.factor),one_of(c("Petal.Length", "Petal.Width")))
iris %>%   mutate_each_(funs(as.factor),one_of(vars))

# Without %>% This works
mutate_each(iris,funs(as.factor), one_of(c("Petal.Length", "Petal.Width"))) %>% summary

很奇怪.. 有什么想法??

THKS

2 个答案:

答案 0 :(得分:24)

今天应该有一个更好的解决方案:

iris %>% mutate_if(is.numeric,as.factor)

答案 1 :(得分:0)

更新了 dplyr 1.06 版及更早版本的答案(在此之前也有一点,但我忘记了版本)

iris %>%
   mutate(across(where(is.numeric) , as.factor))

mutate_if()(和其他)已被 across()across() / where() 组合在 mutate 中取代。