Skip to content

Instantly share code, notes, and snippets.

@grosscol
Created February 1, 2019 13:55
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 grosscol/d6c9c0408391c3e00645dd602e3e3f8a to your computer and use it in GitHub Desktop.
Save grosscol/d6c9c0408391c3e00645dd602e3e3f8a to your computer and use it in GitHub Desktop.
string combinations
library(gtools)
demo_input <- c("fname midname lname","doe john e") #only 2 'names' in this example list.
split_list <- strsplit(demo_input, " |-")
make_combinations <- function(x){
# Use permutations from the gtools package
name_grid <- permutations(3,3,x)
apply(X=name_grid, MARGIN=1, FUN=paste0, collapse=' ')
}
lapply(X=split_list, FUN=`make_combinations`)
@grosscol
Copy link
Author

grosscol commented Feb 1, 2019

> lapply(X=split_list, FUN=`make_combinations`)
[[1]]
[1] "fname lname midname" "fname midname lname" "lname fname midname" "lname midname fname" "midname fname lname" "midname lname fname"

[[2]]
[1] "doe e john" "doe john e" "e doe john" "e john doe" "john doe e" "john e doe"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment