How to identify all columns that contain binary representation
We could use colSums
with stack
stack(colSums(dframe[-1] > 1) == 0)
# values ind
#1 FALSE Google
#2 TRUE Yahoo
#3 TRUE Amazon
Some other methods using dplyr
library(dplyr)
dframe %>% summarise_at(-1, ~all(. < 2))
Or using apply
!apply(dframe[-1] > 1, 2, any)
A tidyverse
approach:
dframe %>%
purrr::map_lgl(~all(.x %in% c(0,1)))
id Google Yahoo Amazon
FALSE FALSE TRUE TRUE
Or if you want it in the exact format:
dframe %>%
purrr::map_lgl(~all(.x %in% c(0,1))) %>%
.[-1] %>%
as.data.frame() %>%
setNames("values")
values
Google FALSE
Yahoo TRUE
Amazon TRUE
An option is
dframe[!Reduce(`|`, lapply(dframe[-1], `>`, 0)),]
You can create your own fnction:
is_binary <- function(x) length(unique(x)) == 2
sapply(dframe, is_binary)
#id Google Yahoo Amazon
#FALSE TRUE TRUE TRUE
If you're actually looking for 0
and 1
binary, you can do:
is_binary <- function(x) all(unique(x) %in% c(0, 1))
sapply(dframe, is_binary)
# id Google Yahoo Amazon
#FALSE FALSE TRUE TRUE