Last active
July 21, 2022 13:10
-
-
Save arcaravaggi/3d1aa1932f138d0ea679c103551a81a8 to your computer and use it in GitHub Desktop.
Reduce multiple columns to single binary vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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