0

I have a matrix mat whose elements are NA, 0, 1, 2. I got an answer about 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, 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 used 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)]
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Ati
  • 55
  • 1
  • 7
  • 1
    @akrun I guess you can find it [there](http://stackoverflow.com/questions/31420791/delete-the-colmuns-which-are-contained-just-na-or-0-or-both-values)... – Cath Jul 16 '15 at 14:56
  • Thanks, But I want add more condition to that which I explain above! I wrote a code similar to what you referred me, but it didn't work properly!! – Ati Jul 16 '15 at 15:13

2 Answers2

1

If I understand correctly your request, you can try:

mat_nonNA <- mat[, apply(mat, 2, function(x){length(unique(x[!is.na(x)])) > 1})]

It looks for columns with more than 1 different non NA value.

The result is:

mat_nonNA
#       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
Cath
  • 23,906
  • 5
  • 52
  • 86
  • I'm so sorry, I changed my question several times!!! this code works great but who can I change it to add a condition to this code for removing the columns witch contain all of four values, means I want to delete the columns which contain all 4 values( 0,1,2,NA) either? thank you so much – Ati Jul 16 '15 at 17:13
  • is it true? mat_nonNA <- mat[, !apply(mat, 2, function(x){length(unique(x[!is.na(x)]))==1}) & !apply(mat, 2, function(x){length(unique(x[!is.na(x)]))==3})] – Ati Jul 16 '15 at 17:28
  • but still I have the columns which contain all of values (NA,0,1,2 in a one column) together! for this reason I use this code but I think my answer is not correct : mat <- mat[, apply(mat, 2, function(x){length(unique(x[!is.na(x)]))>1 &length(unique( x[!is.na(x)]))<3 })] – Ati Jul 16 '15 at 18:02
  • @Ati maybe you should try to better explain what you want then because, based on what you're saying in the question, it is normal that the columns with all possible values are still there (they have "variation in their values"). If you just want the column which have only 2 different values (not 1 but not 3) apart from NA, you can just do `mat[, apply(mat, 2, function(x){length(unique(x[!is.na(x)])) == 2})]` – Cath Jul 17 '15 at 06:24
0

I'm pretty sure you can do it directly, but it would be hard to understand such a complicated condition. Instead, try evaluating the conditions separately first:

zeroOrNA <- apply(is.na(mat) | mat == 0, 2, all)
oneOrNA <- apply(is.na(mat) | mat == 1, 2, all)
twoOrNA <- apply(is.na(mat) | mat == 2, 2, all)

mat_nonNA <- mat[ , !(zeroOrNA | oneOrNA | twoOrNA)]
Felipe Gerard
  • 1,552
  • 13
  • 23