Skip to content

Instantly share code, notes, and snippets.

@arcaravaggi
Last active July 21, 2022 13:10
Show Gist options
  • Save arcaravaggi/3d1aa1932f138d0ea679c103551a81a8 to your computer and use it in GitHub Desktop.
Save arcaravaggi/3d1aa1932f138d0ea679c103551a81a8 to your computer and use it in GitHub Desktop.
Reduce multiple columns to single binary vector
# Function to reduce multiple columns to single binary vector
# c = data frame or columns of interest
binRC <- function(c){
d <- as.integer(rowSums(c)>0)
return(d)
}
# Here's an example, using a randomly generated data frame
set.seed(42)
df <- data.frame(col1 = rbinom(n=100, size=1, prob=0.05),
col2 = rbinom(n=100, size=1, prob=0.25),
col3 = rbinom(n=100, size=1, prob=0.25))
binRows(df[,1:3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment