Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify all columns that contain binary representation [duplicate]

Tags:

r

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
like image 205
Nathalie Avatar asked Aug 16 '19 12:08

Nathalie


People also ask

How to store binary representation of any number in C++?

// 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.

What is the range of I in a binary representation?

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.

What are string and binary data types in SQL Server?

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)

How to print binary representation of ‘NUM’ in Python?

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. ...


4 Answers

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)
like image 190
Ronak Shah Avatar answered Oct 17 '22 18:10

Ronak Shah


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
like image 33
NelsonGon Avatar answered Oct 17 '22 19:10

NelsonGon


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 
like image 2
patL Avatar answered Oct 17 '22 17:10

patL


An option is

dframe[!Reduce(`|`, lapply(dframe[-1], `>`, 0)),]
like image 2
akrun Avatar answered Oct 17 '22 19:10

akrun