Skip to content

Instantly share code, notes, and snippets.

@badbye
Last active August 29, 2015 14:12
Show Gist options
  • Save badbye/df2534324e3bc3afa17d to your computer and use it in GitHub Desktop.
Save badbye/df2534324e3bc3afa17d to your computer and use it in GitHub Desktop.
extract all the sub set of a set
sub_index = function(x){
n = length(x)
com = paste0(rep('0:1,', n), collapse='')
com = paste0('expand.grid(',
substr(com, 1, nchar(com)-1),
')', collapse = '')
eval(parse(text = com))
}
sub_set = function(x){
index = sub_index(x)
apply(sub_index(x), 1, function(index) x[as.logical(index)])
}
x = 1:2
sub_set(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment