I have an assigment in which I have absolutely no idea how to start to make it work.
I have to create variations of list of words, where will be replaced every character (between 1st and last) with '*' on different positions.
It should look something like this:
input: c('smog', 'sting')
desired output: 's*og', 'sm*g', 's**g', 's*ing', 'st*ng', 'sti*g', 's***g'
Any idea how to achieve something like this?
Thank you very much
UPDATE I've found this solution:
s <- c( 'smog')
f <- function(x,y) {substr(x,y,y) <- "*"; x}
g <- function(x) Reduce(f,x,s)
unlist(lapply(1:(nchar(s)-2),function(x) combn(2:(nchar(s)-1),x,g)))
output:
[1] "s*og" "sm*g" "s**g"
the only problem with this is, that it works only when there is one word in the string, not several