Skip to content

Instantly share code, notes, and snippets.

@willjobs
Last active October 2, 2017 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willjobs/1495f232e62ec0f63da38f71e9821e61 to your computer and use it in GitHub Desktop.
Save willjobs/1495f232e62ec0f63da38f71e9821e61 to your computer and use it in GitHub Desktop.
R script to enumerate all binary strings of a certain length and type. Useful for getting all possible combinations of columns, for example.
# "enumerate_bin("1xx000") generates a vector of all binary strings: c("100000","101000","110000","111000")
enumerate_bin <- function(input_str) {
strlen<-nchar(input_str)
for(i in 1:strlen) {
if(tolower(substr(input_str,i,i))=="x") {
str0 <- paste(substr(input_str,1,i-1),"0",substr(input_str,i+1,strlen),sep="")
str1 <- paste(substr(input_str,1,i-1),"1",substr(input_str,i+1,strlen),sep="")
return(c(enumerate_bin(str0), enumerate_bin(str1)))
}
}
return(input_str)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment