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
How about the following? t1 <- matrix(letters[1:9], nrow=3, byrow=TRUE) t1 t2 <- t(apply(t1, 1, function(x) match(x, letters))) t2 Regards, Andrew C. Ward CAPE Centre Department of Chemical Engineering The University of Queensland Brisbane Qld 4072 Australia andreww at cheque.uq.edu.au Quoting Robin Hankin <r.hankin at auckland.ac.nz>:> 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 >
On Fri, 02-May-2003 at 04:42AM +0000, Andrew C. Ward wrote: |> How about the following? |> t1 <- matrix(letters[1:9], nrow=3, byrow=TRUE) |> t1 |> t2 <- t(apply(t1, 1, function(x) match(x, letters))) |> t2 Or slightly more simply t2 <- apply(t1, 2, function(x) match(x, letters)) Might not work for non-unique letters. best -- Patrick Connolly HortResearch Mt Albert Auckland New Zealand Ph: +64-9 815 4200 x 7188 ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~ I have the world`s largest collection of seashells. I keep it on all the beaches of the world ... Perhaps you`ve seen it. ---Steven Wright ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
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