Re: [R] Generate Variable Length Strings from Various Sources

2014-01-18 Thread Burhan ul haq
Hi Rainer,

Thanks for the tip.

Your suggestion works perfectly, however as per the R Mantra of avoiding
for loops,  I propose the following this alternate:

# 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 )

my.g = function(len,src)
{
  my.s = src[[ sample( 1:4, 1 ) ]]
  tmp = sample(my.s,len,rep=TRUE)
  n1 = paste(tmp,collapse=)
  n1
} # end

sapply(v.length,my.g,src)


// 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.


Re: [R] Generate Variable Length Strings from Various Sources

2014-01-18 Thread Jeff Newmiller
People who subscribe to an avoid-for-loops mantra are often missing the 
point. Converting for loops to indexing can often yield speed improvements. 
Converting for loops to apply functions does not improve speed. (If you think 
it does, you are probably handling memory allocation differently.) Worse, in 
some cases it can reduce readability, so doing it because of a mantra can 
actually harm your code.

Also, posting in HTML is hardly the R-help way (see the Posting Guide).
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Burhan ul haq ulh...@gmail.com wrote:
Hi Rainer,

Thanks for the tip.

Your suggestion works perfectly, however as per the R Mantra of
avoiding
for loops,  I propose the following this alternate:

# 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 )

my.g = function(len,src)
{
  my.s = src[[ sample( 1:4, 1 ) ]]
  tmp = sample(my.s,len,rep=TRUE)
  n1 = paste(tmp,collapse=)
  n1
} # end

sapply(v.length,my.g,src)


// 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.

__
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.


[R] Generate Variable Length Strings from Various Sources

2014-01-15 Thread Burhan ul haq
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.


Re: [R] Generate Variable Length Strings from Various Sources

2014-01-15 Thread Rainer Schuermann
### 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]  2212 212  HFG  zzz  11   TRM  FGBA zz   LLLR
[21] 211  21   SSKR BEDD NK   LO   221  GDE  MNOT zz  
[31] DHD   RMSS PSO  111  zz   EFFF JAB  BBB  QQRN
[41] FDG   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))

__
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.