Select 选择dplyr具有因子“的多个列;否&引用;是”;水平
我想选择所有具有两个级别的因子列(“是”、“否”)。 我想为此使用dpylr,但无法解决问题Select 选择dplyr具有因子“的多个列;否&引用;是”;水平,select,dplyr,multiple-columns,Select,Dplyr,Multiple Columns,我想选择所有具有两个级别的因子列(“是”、“否”)。 我想为此使用dpylr,但无法解决问题 AB %>% select_if(.predicate = function(x) length(levels(x))==2 & unique(x) %in% c("No", "Yes")) %c('No','Yes')中的unique(x)%返回与unique(x)长度相同的向量,而不是标量。我认为最好使用setequal(x,c('No','Yes')),如下所示:
AB %>%
select_if(.predicate = function(x) length(levels(x))==2 & unique(x) %in% c("No", "Yes"))
%c('No','Yes')中的unique(x)%
返回与unique(x)
长度相同的向量,而不是标量。我认为最好使用setequal(x,c('No','Yes'))
,如下所示:
library(dplyr)
# generate the dataframe with different factor levels
n<-100
no_yes <- sample(c('No','Yes'), n, replace = T)
no_yes_maybe <- sample(c('No','Yes','Maybe'), n, replace = T)
no <- sample(c('No'), n, replace = T)
no_maybe <- sample(c('No','Maybe'), n, replace = T)
AB<-data.frame(
no_yes, # only this column should get returned
no_yes_maybe,
no,
no_maybe,
stringsAsFactors = T
)%>%as.tbl
# function to return TRUE if column has only No/Yes factors.
desired_levels <- c('No','Yes')
predicate_function <- function(x) setequal(levels(x),desired_levels)
# use dplyr to select columns with desired factor levels
AB%>%select_if(predicate_function)
库(dplyr)
#生成具有不同因子级别的数据帧
NCA能否提供样本数据作为起点?如果您不提供AB
是什么,即使它只有5-10行,也很难帮助您解决问题。