[R] removing the columns with 0 or NA or 1or NA or 2 or NA

Jim Lemon drjimlemon at gmail.com
Fri Jul 17 12:55:36 CEST 2015


Hi Lida,
I think that your "matrix" is actually a data frame, so try this:

mat[,sapply(mat,function(x) var(x,na.rm=TRUE)>0)]

Jim


On Fri, Jul 17, 2015 at 12:58 AM, Lida Zeighami <lid.zigh at gmail.com> wrote:
> I have ma matrix which its elements are NA,0,1,2 ! I got my answer bout
> removing the columns with 0 or NA or both values but now I want to add
> additional condition for deleting the columns! I have to delete the columns
> which contain the same value. delete the columns with NA or 0 or both and
> the columns with NA or 1 or both and the column with NA or 2 or both (I
> should keep the columns which have variation in their values)! I use this
> code but didn't work properly:
>
> mat_nonNA<- mat[, !apply((is.na(mat) | mat == 0) & (is.na(mat) | mat==1) &(
> is.na(mat) | mat==2), 2, all)]
>
> mat
>          1:110590170    1:110888172     1:110906406   1:110993854
>  1:110996710   1:111144756
> A05363           1                       1                     1
>           2                         NA
> 0
> A05370           0                       1
> 0                NA                         0                     NA
> A05380           1
>          NA                   2                NA
>   NA
> 0
> A05397           0                        1
> 0                NA                         0                       2
> A05400           2                        1
> 0                 2                           0                       0
> A05426
> 0                       NA                     NA             NA
> 0                       1
>
> my out put should be like below:
>
>        1:110590170         1:110906406          1:111144756
> A05363           1                         1
> 0
> A05370           0                          0
> NA
> A05380           1                          2
> 0
> A05397           0
> 0                                 2
> A05400           2                          0
>           0
> A05426           0                         NA
> 1
>
> Thanks for your help
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list