[R] Converting Numerical Matrix to List of Strings
jim holtman
jholtman at gmail.com
Sun Jan 11 17:18:28 CET 2009
try this:
> mapping <- c('0'='a', '1'='c', '2'='g', '3'='t')
> x <- matrix(sample(0:3, 30, TRUE), ncol=3)
> x
[,1] [,2] [,3]
[1,] 3 1 1
[2,] 1 3 2
[3,] 1 1 1
[4,] 1 1 1
[5,] 2 1 3
[6,] 1 3 0
[7,] 1 3 2
[8,] 3 1 0
[9,] 0 3 0
[10,] 3 3 0
> apply(x, 1, function(z){
+ paste(mapping[as.character(z)], collapse='')
+ })
[1] "tcc" "ctg" "ccc" "ccc" "gct" "cta" "ctg" "tca" "ata" "tta"
>
>
On Sun, Jan 11, 2009 at 10:38 AM, Gundala Viswanath <gundalav at gmail.com> wrote:
> Hi all,
>
> Given a matrix:
>
>> mat
>
> [,1] [,2] [,3]
> [1,] 0 0 0
> [2,] 3 3 3
> [3,] 1 1 1
> [4,] 2 1 1
>
>
> How can I convert it to a list of strings:
>
>> desired_output
> [1] "aaa" "ttt" "ccc" "gcc"
>
>
> In principle:
>
> 1. Number of Column in matrix = length of string (= 3)
> 2. Number of Row in matrix = length of vector ( = 4).
> 3. Character "a" encode as "0",
> "c" -> "1",
> "g" -> "2",
> "t" -> "3"
>
>
> Length of strings are assumed to be uniform within the vector,
> and it can be greater than 3 (up to 40 characters).
>
>
> - Gundala Viswanath
> Jakarta - Indonesia
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list