Having a dataframe like this:
dframe <- data.frame(id = c(1,2,3), Google = c(2,1,1), Yahoo = c(0,1,1), Amazon = c(1,1,0))
How is it possible to test every column if it contains binary (0 and 1) representation or not (max number in every row is not greater than 1)
Example
colname, binary_status
Google, False
Yahoo, True
Amazon, True
// This code is contributed by Surbhi Tyagi. We can use the bitset class of C++ to store the binary representation of any number (positive as well as a negative number). It offers us the flexibility to have the number of bits of our desire, like whether we want to have 32-bit binary representation of just an 8-bit representation of a number.
If i'th bit is set for position i in integer n, then set i'th bit to 1 in the binary representation of n; otherwise, 0. The i ranges from 0 to 31 for a 32–bit integer.
In SQL Server string and binary columns are with the following data types: text, ntext, varchar, nvarchar, char, nchar, binary, varbinary, image. The query below lists all columns with string and binary data types. Scope of rows: all columns containing string and binary data types in the database (schema)
1 Method 1: Iterative. For any number, we can check whether its ‘i’th bit is 0 (OFF) or 1 (ON) by bitwise ANDing it with “2^i” (2 raise to i). 2 Method 2: Recursive. Following is recursive method to print binary representation of ‘NUM’. 3 Method 3: Recursive using bitwise operator 4 Method 4: Inbuilt library of Python. ...
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
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
An option is
dframe[!Reduce(`|`, lapply(dframe[-1], `>`, 0)),]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With