Given a “UK postal code” pattern, for example “A9 9AA”, where “A” is a letter placeholder and “9” is a number placeholder, I want to generate random zip code strings like “H8 4GB”, the letters can be any uppercase letter and numbers from 0 to 9.
So, if the pattern is "AA9A 9AA", then I need strings like "WC1A 9LK". I ignore while creating "real" zip codes, so I don't worry if "WC1A" is valid external code.
I tried to get the functions from the stringi package to work, but the problem is that replacing or matching "A" in the template will only replace the first replacement, for example:
stri_replace_all_fixed("A9 9AA",c("A","A","A"), c("X","Y","Z"), vectorize_all=FALSE) [1] "X9 9XX"
therefore, it does not replace each “A” with each element from the replacement vector (but this is by design).
Maybe there is something in stringi or the R base that I missed - I would like to leave it in these packages so that I don't inflate what I'm working on.
The brute force method is to break the pattern, make replacements, insert the result back together, but I would like to see if there is a faster, naturally vectorized solution.
So, we summarize:
foo("A9 9AA") # return like "B6 5DE" foo(c("A9 9AA","A9 9AA","A9A 9AA")) # returns c("Y6 5TH","D4 8JH","W0Z 3KQ")
Here's a non-vector version that relies on building an expression and evaluating it ...
random_pc <- function(fmt){ cc = gsub(" ",'c(" ")',gsub("9","sample(0:9,1)",gsub("A","sample(LETTERS,1)",strsplit(fmt,"")[[1]]))) paste(eval(parse(text=paste0("c(",paste(cc,collapse=","),")"))),collapse="") } > random_pc("AA9 9AA") [1] "KO6 1AY"