首页 > 解决方案 > 如何识别包含二进制表示的所有列

问题描述

有这样的数据框:

dframe <- data.frame(id = c(1,2,3), Google = c(2,1,1), Yahoo = c(0,1,1), Amazon = c(1,1,0))

如何测试每一列是否包含二进制(0和1)表示(每行中的最大数不大于1)

例子

colname, binary_status
Google, False
Yahoo, True
Amazon, True

标签: r

解决方案


我们可以colSums使用stack

stack(colSums(dframe[-1] > 1) == 0)

#  values    ind
#1  FALSE Google
#2   TRUE  Yahoo
#3   TRUE Amazon

其他一些使用方法dplyr

library(dplyr)
dframe %>% summarise_at(-1, ~all(. < 2)) 

或使用apply

!apply(dframe[-1] > 1, 2, any)

推荐阅读