r - 如何识别包含二进制表示的所有列
问题描述
有这样的数据框:
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
解决方案
我们可以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)
推荐阅读
- mongodb - 如何通过 MongoDB 中的嵌套字段连接到集合
- javascript - mongodb - 使用 COLLSCAN 而不是索引的 $lookup 管道
- python - 无论我尝试什么,BeautifulSoup4 都找不到表
- javascript - 即使在地图移动期间也保持标记居中
- spring - 过滤通量反应器中的重复对象
- function - 为什么这两个表达式不等价,一个给出错误?
- python - 在python中绘制多个数字
- c# - 如何从作为服务器的 SslStream 获取请求的目标主机
- c++ - 如何让我的程序正确地将数据传递给我的数组?(家庭作业)
- csv - 简化查询 Neo4j