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

From: Barry Rowlingson <b.rowlingson_at_lancaster.ac.uk>
Date: Tue, 29 Jan 2008 14:06:38 +0000

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



R-help_at_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. Received on Tue 29 Jan 2008 - 16:18:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Jan 2008 - 17:30:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive