Burhan ul haq
2014-Jan-15 15:15 UTC
[R] Generate Variable Length Strings from Various Sources
Hi,
I am trying to generate variable length strings from variable sources as
follows:
# ------------------------------------>8
8<-------------------------------------------------
# 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))
# ------------------------------------>8
8<-------------------------------------------------
# ISSUE - Details:
How to randomly pass a source, either of source 1, 2, 3 or 4.
I have tried with and without the quotes, but it does not work. Without
quotes, it works, but then letters are chosen from a randomized mix of all
sources, such as "A" from src.1, "z" from src.3, whereas I
want, only 1
source at a time, for a name.
# Result with quotes:> dput(r1)
c("src.4src.1src.4", "src.1src.4src.4",
"src.4src.3", "src.4src.3src.4",
"src.4src.4", "src.1src.4src.4",
"src.1src.1src.4src.3",
"src.1src.1src.1src.4",
"src.4src.1src.4src.3", "src.1src.4src.4",
"src.3src.1src.4",
"src.4src.3src.1", "src.1src.3src.1src.3",
"src.4src.1src.1src.1",
"src.4src.3src.4", "src.3src.3src.4",
"src.1src.3src.1src.1",
"src.3src.3src.1src.4", "src.1src.1src.3",
"src.3src.4src.3",
"src.3src.4src.3", "src.4src.1src.4src.3",
"src.1src.3src.4src.3",
"src.4src.1", "src.1src.3src.4",
"src.3src.4src.3", "src.4src.3",
"src.3src.3", "src.3src.4", "src.4src.4",
"src.1src.4src.1src.4",
"src.1src.4src.1", "src.3src.3",
"src.3src.1src.4", "src.1src.3src.1src.3",
"src.3src.4src.1", "src.4src.3src.1",
"src.1src.4src.1src.4",
"src.3src.4src.1src.4", "src.1src.3src.4src.3",
"src.4src.4src.3",
"src.4src.1src.3src.1", "src.3src.3",
"src.1src.4src.4", "src.4src.1src.4",
"src.3src.3", "src.1src.1", "src.3src.1src.1",
"src.1src.3",
"src.3src.4src.4src.3")
# Result without quotes:> dput(r1)
c("IGC", "B1I", "BB", "G1C",
"AE", "GBE", "2DJA", "CIAG",
"IGE1",
"G22", "EFD", "DGI", "BFzB",
"1FI1", "JFH", "EJA", "IEzF",
"FJGB",
"I2z", "IFC", "FFE", "IzJE",
"FJ1I", "BI", "FJG", "EJB",
"GF",
"AD", "IJ", "IE", "BCGA",
"G1F", "FF", "GBB", "FGCJ",
"1ID",
"FzA", "GJ12", "FC2G", "FCJ2",
"zIJ", "GHFB", "AI", "EFB",
"2GI",
"FF", "22", "EI1", "EG",
"FC21")
Thanks in advance.
/ Cheers
[[alternative HTML version deleted]]
Rainer Schuermann
2014-Jan-15 18:21 UTC
[R] Generate Variable Length Strings from Various Sources
### 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))