[R] Converting Numerical Matrix to List of Strings

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Sun Jan 11 17:31:16 CET 2009


one way is the following:

mat <- matrix(sample(0:3, 12, TRUE), 4, 3)
strg <- c("a", "c", "g", "t")

out <- strg[mat + 1]
dim(out) <- dim(mat)
apply(out, 1, paste, collapse = "")


I hope it helps.

Best,
Dimitris


Gundala Viswanath 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.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list