Create all combinations of letter substitution in string
I have a string "ECET" and I would like to create all the possible strings where I substitute one or more letters (all but the first) with "X". So in this case my result would be: > result [1] "EXET" "ECXT" "ECEX" "EXXT" "EXEX" "ECXX" "EXXX" Any ideas as to how to approach the issue? This is not just create the possible combinations/permutations of "X" but also how to combine them with the existing string. Using the FUN argument of combn: a <- "ECET" fun <- function(n, string) { combn(nchar(string), n, function(x) { s <- strsplit(string, '')[[1]] s[x] <- 'X' paste(s, collapse = '') } ) } lapply(seq_len(nchar(a)), fun, string = a) [[1]] [1] "XCET" "EXET" "ECXT" "ECEX" [[2]] [1] "XXET" "XCXT" "XCEX" "EXXT" "EXEX" "ECXX" [[...