【发布时间】:2019-04-17 14:55:04
【问题描述】:
我正在尝试找到一种使用 dplyr 将二进制数值变量转换为因子的快速方法。
我有一个具有这种结构的数据集:
library(dplyr)
f<-as_tibble(data.frame(col1=c(1,1,0),col2=c("ham","spam","spam"),col3=c(1,2,8),col4=c(1,0,0)))
目前,我已经尝试使用 n_distinct
g<-f %>% select_if(is.numeric) %>% sapply(n_distinct)
但我不知道如何只过滤掉带有n_distinct == 2 的那些列。需要明确的是,我的最终输出应该是:
names(g[g==2])
[1] "col1" "col4"
有什么想法吗? 谢谢
【问题讨论】: