使用具有因子“否”,“是”级别的dplyr选择多个列

时间:2017-12-29 09:31:37

标签: select dplyr multiple-columns

我想选择具有两个级别的所有因子列(“是”,“否”)。 我想为此使用dpylr,但无法解决问题。

    AB %>%
    select_if(.predicate = function(x) length(levels(x))==2 & unique(x) %in% c("No", "Yes"))

1 个答案:

答案 0 :(得分:1)

unique(x) %in% c('No','Yes')返回与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)