[R] Conversion of matrix in r to integer

arun smartpink111 at yahoo.com
Thu Aug 1 18:25:53 CEST 2013


Hi,
It is better to keep it as a data.frame as matrix cannot hold different classes together, so it automatically converts the other columns into "character" as the first column "gene" is "character".


dat1<- read.table("RP_matrix_RF_ZPTvsPGR.txt",sep="",header=TRUE,stringsAsFactors=FALSE)
dat1[,-1]<- lapply(lapply(dat1[,-1],round),as.integer)
 str(dat1)
#'data.frame':    28597 obs. of  8 variables:
# $ gene : chr  "XLOC_000001" "XLOC_000002" "XLOC_000003" "XLOC_000004" ...
# $ ZPT.1: int  3516 342 2000 143 0 0 0 0 0 7 ...
# $ ZPT.0: int  626 82 361 30 0 0 0 0 0 1 ...
# $ ZPT.2: int  1277 185 867 67 0 0 0 0 0 5 ...
# $ ZPT.3: int  770 72 438 37 0 0 0 0 0 3 ...
# $ PGR.1: int  2603 304 195 66 0 1 0 0 0 0 ...
# $ PGR.0: int  1534 175 80 49 0 0 1 0 0 0 ...
# $ PGR.2: int  1764 208 109 54 0 0 1 0 0 1 ...


In case you want it as matrix: then
mat1<- as.matrix(dat1[,-1])
 row.names(mat1)<- dat1[,1]
 str(mat1)
# int [1:28597, 1:7] 3516 342 2000 143 0 0 0 0 0 7 ...
# - attr(*, "dimnames")=List of 2
#  ..$ : chr [1:28597] "XLOC_000001" "XLOC_000002" "XLOC_000003" "XLOC_000004" ...
#  ..$ : chr [1:7] "ZPT.1" "ZPT.0" "ZPT.2" "ZPT.3" ...
head(mat1)
#            ZPT.1 ZPT.0 ZPT.2 ZPT.3 PGR.1 PGR.0 PGR.2
#XLOC_000001  3516   626  1277   770  2603  1534  1764
#XLOC_000002   342    82   185    72   304   175   208
#XLOC_000003  2000   361   867   438   195    80   109
#XLOC_000004   143    30    67    37    66    49    54
#XLOC_000005     0     0     0     0     0     0     0
#XLOC_000006     0     0     0     0     1     0     0


A.K.




________________________________
From: Vivek Das <vd4mmind at gmail.com>
To: arun <smartpink111 at yahoo.com>; R help <r-help at r-project.org> 
Sent: Thursday, August 1, 2013 11:48 AM
Subject: Conversion of matrix in r to integer



Hi,
 I have a matrix which which I need for some analysis and that package in R only works on integer values. My data.frame is not integer value . I want to convert the values to the nearest  integer values for my matrix. Can you tell me how to do it in r. I am sending you the matrix where you can see a lot of decimal point numbers and all of them needs to be converted to integer values  


----------------------------------------------------------

Vivek Das
PhD Student in Computational Biology
Giuseppe Testa's Lab
European School of Molecular Medicine
IFOM-IEO Campus
Via Adamello, 16
Milan, Italy

emails: vivek.das at ieo.eu
            vchris_05 at yahoo.co.in
            vd4mmind at gmail.com



More information about the R-help mailing list