[R] letters to numbers conversion

Tony Plate tplate at blackmesacapital.com
Fri May 2 07:08:48 CEST 2003


If you're looking for something that works with matrices or arrays you 
could use the following.  Note that you would want to substitute LETTERS 
for letters if your original data was in uppercase.  The stuff with 
dimnames isn't necessary for this example, but it don't hurt and it will 
preserve dimnames when the original data has them.

 > x <- matrix(letters[1:9],ncol=3)
 > x
      [,1] [,2] [,3]
[1,] "a"  "d"  "g"
[2,] "b"  "e"  "h"
[3,] "c"  "f"  "i"
 > match(x, letters)
[1] 1 2 3 4 5 6 7 8 9
 > array(match(x, letters), dim=dim(x), dimnames=dimnames(x))
      [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
 >

hope this helps,

Tony Plate

At Friday 04:28 PM 5/2/2003 +1200, Robin Hankin wrote:
>Hello List
>
>How do I turn
>
>R> simple.example.alphabetic
>      [,1] [,2] [,3]
>[1,] "a"  "b"  "c"
>[2,] "d"  "e"  "f"
>[3,] "g"  "h"  "i"
>
>into
>
>R> simple.example.numeric
>      [,1] [,2] [,3]
>[1,]    1    2    3
>[2,]    4    5    6
>[3,]    7    8    9
>
>[ie "a" becomes 1, ..., "z" becomes 26]
>?
>
>
>
>
>
>
>--
>
>Robin Hankin, Lecturer,
>School of Geography and Environmental Science
>Tamaki Campus
>Private Bag 92019 Auckland
>New Zealand
>
>r.hankin at auckland.ac.nz
>tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help



More information about the R-help mailing list