Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # vector
- x = c('1','2','3')
- # i want a vector of all combinations of these strings, but i want to be able to say I want all 2 way or three way combs
- # two-way is easy
- get_all_combs <- function(x){
- return(expand.grid(x,x) %>%
- filter(Var1 != Var2) %>%
- unite(., 'y', everything(), sep = '') %>%
- pull(y))
- }
- get_all_combs(x)
- # becomes much harder with three way
- get_all_combs <- function(x, vec_len = 2){
- # set command
- return(expand.grid(rep(list(x), vec_len)) %>%
- filter(apply(., 1, function(x)length(unique(x))) == vec_len) %>%
- unite(., 'y', everything(), sep = '') %>%
- pull(y))
- }
- # bit gnarly but gets there - the apply is very stressful but not sure how else to do it
- get_all_combs(x, vec_len = 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement