[R] remove 0 and NA values

Daniel Nordlund djnordlund at frontier.com
Tue Jul 14 07:52:28 CEST 2015


On 7/13/2015 6:02 PM, Lida Zeighami wrote:
> Hi Dan,
>
> Thanks for reply,
> Sorry the format of matrix is ruiend!
> Yes, this matrix is 6×6 but my orginal matrix is so biger than this!!
>
> No, I don't think so your code do that for me!
> I want to remove the columns which the sum of their values are equal to
> zero!
>
> On Jul 13, 2015 5:31 PM, "Daniel Nordlund" <djnordlund at frontier.com
> <mailto:djnordlund at frontier.com>> wrote:
>
>     On 7/13/2015 3:01 PM, Lida Zeighami wrote:
>
>         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
>
>
>     First, you matrix appears to be 6x6.  That being said, does this get
>     you what you want?
>
>     mat[, -which(summat[,1] ]
>
>
>     Dan
>
>     --
>     Daniel Nordlund
>     Bothell, WA USA
>

Lida,

I seem to have cut-and-pasted something very badly, and for that I 
apologize.  Here is a revised version:

mat <- structure(c(0L, 0L, 1L, 0L, 2L, 0L, 0L, 0L, NA, 0L, 0L, 0L, 0L,
0L, 2L, 0L, 0L, NA, 0L, 0L, 0L, 1L, 0L, 0L, NA, 0L, NA, 0L, 0L,
0L, 0L, NA, 0L, 2L, 0L, 0L), .Dim = c(6L, 6L), .Dimnames = list(
c("A05363", "A05370", "A05380", "A05397", "A05400", "A05426"), 
c("X1.110590170", "X1.110888172", "X1.110906406", "X1.110993854", 
"X1.110996710", "X1.111144756")))
summat <- colSums(mat,na.rm = TRUE)
mat[,-which(summat==0)]
        X1.110590170 X1.110906406 X1.110993854 X1.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
 >

Hope this is more helpful,

Dan

-- 
Daniel Nordlund
Bothell, WA USA



More information about the R-help mailing list