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]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to