[R] remove 0 and NA values

Lida Zeighami lid.zigh at gmail.com
Tue Jul 14 00:01:42 CEST 2015


Hi there,

I have a matrix which its elements are 0, 1,2,NA
I want to remove the columns which the colsums are equal to 0 or NA and
drop these columns from the original matrix and create the new matrix for
the nonzero and NA value?
 (I think I have consider na.rm=True and remove the colums with colsum=0,
 because if I consider na.rm=False all the values of my colsums get NA)

this is my matrix format:

mat[1:5,1:5]

           1:110590170    1:110888172     1:110906406   1:110993854
 1:110996710   1:111144756
A05363           0                        0                     0
        0                         NA                     0
A05370           0                        0                     0
        0                         0                     NA
A05380           1
         NA                   2                  0
     NA                     0
A05397           0                        0
0                  1                         0                       2
A05400           2                        0                     0
          0                         0                        0
A05426           0
0                     NA               0
0                        0



summat <-  colSums(mat,na.rm = TRUE)

> head(summat)
                        [,1]
1:110590170     3
1:110888172     0
1:110906406      2
1:110993854     1
1:110996710     0
1:111144756     2

 The 2nd and 5th columns have colsum=0 so I Ishould remove them from the
met and keep the rest of columns in  another matrix.

my out put should be like below:

metnonzero

       1:110590170         1:110906406          1:110993854
  1:111144756
A05363           0                          0
0                                   0
A05370           0                          0
0                                  NA
A05380           1                          2
0                                   0
A05397           0
0                  1                                   2
A05400           2                          0
0                                   0
A05426           0                          NA
0                                   0

would you please let me know how can I do that?

Many thanks,
Lid

	[[alternative HTML version deleted]]



More information about the R-help mailing list