[R] Generate Variable Length Strings from Various Sources

Rainer Schuermann Rainer.Schuermann at gmx.net
Wed Jan 15 19:21:30 CET 2014


### How I would do it:

# container for the result
res <- NULL

# number of strings to be created
n <- 50

# random length of each string
v.length = sample( c( 2:4), n, rep = TRUE )

# letter sources
src.1 = LETTERS[ 1:10 ]
src.2 = LETTERS[ 11:20 ]
src.3 = "z"
src.4 = c( "1", "2" )

# turn into a list
src <- list( src.1, src.2, src.3, src.4 )

# use a loop
for( i in 1:n )
{
  res[[i]] <- paste( sample( src[[ sample( 1:4, 1 ) ]], v.length[ i ], rep = TRUE ), collapse = "" )
}
res <- unlist( res )
 [1] "RLOK" "22"   "CCA"  "IEC"  "zz"   "111"  "12"   "zzz"  "KOS"  "12"  
[11] "zzzz" "2212" "212"  "HFG"  "zzz"  "11"   "TRM"  "FGBA" "zz"   "LLLR"
[21] "211"  "21"   "SSKR" "BEDD" "NK"   "LO"   "221"  "GDE"  "MNOT" "zz"  
[31] "DHD"  "2222" "RMSS" "PSO"  "111"  "zz"   "EFFF" "JAB"  "BBB"  "QQRN"
[41] "FDG"  "zzzz" "zz"   "CDE"  "111"  "zz"   "zzz"  "GAB"  "zzz"  "JGGD"




On Wednesday 15 January 2014 20:15:03 Burhan ul haq wrote:
> # Function to generate a string, given:
> #   its length(passed as len)
> #   and the source(passed as src)
> my.f = function(len,src)
> {
> tmp = sample(src,len,rep=FALSE)
> n1 = paste(tmp,collapse="")
> n1
> } # end
> 
> # count
> n=50
> 
> # length of names, a variable indicating string length
> v.length = sample(c(2,3,4),n,rep=TRUE)
> 
> # letter sources
> src.1 = LETTERS[1:10]
> src.2 = LETTERS[11:20]
> src.3 = "z"
> src.4 = c("1","2")
> 
> # Issue
> #s.ind = sample(c("src.1","src.2"),n,rep=TRUE)
> s.ind = sample(c(src.1,src.3,src.4),n,rep=TRUE)
> 
> # Generate "n" strings, whose length is given by v.length, and randomly
> using sources (src1 to 4)
> unlist(lapply(v.length,my.f,s.ind))




More information about the R-help mailing list