[R] how to generate sequence "a" - "z"

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Jan 29 15:06:38 CET 2008


Bernd Weiss wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> skestin schrieb:
> | I suppose it's very simple but I can't find the way to generate a
> sequence of
> | characters, e.g. from "a" to "z".
> | Could you please help me with this?
> 
> ?letters
> letters
> 

  "letters" is okay for just that sequence, or parts of it, but might 
not be general enough for the original poster...

  I just discovered 'charToInt' and 'intToChar' in the R.oo package:

  > intToChar(65:68)
  [1] "A" "B" "C" "D"

  Then you can define cseq:

  > cseq =
  function(from,to,by=1){
  from=charToInt(from)
  to=charToInt(to)
  intToChar(seq(from,to,by))
  }

  and do:
  > cseq('A','G')
   [1] "A" "B" "C" "D" "E" "F" "G"
  > cseq('A','G',2)
   [1] "A" "C" "E" "G"
  > cseq('a','A',-2)
   [1] "a" "_" "]" "[" "Y" "W" "U" "S" "Q" "O" "M" "K" "I" "G" "E" "C" "A"

  Note this uses ASCII codes and not whatever your language alphabet is.

  I'd overload ':' for this and then you could do 'a':'z', but ':' heads 
into .Primitive territory...

Barry



More information about the R-help mailing list