removing columns with 0 or NA or the same values in R? -


i have matrix mat elements na, 0, 1, 2. got answer removing columns 0 or na or both values want add additional condition deleting columns.
have delete columns contain same value, delete columns na or 0 or both, columns na or 1 or both , column na or 2 or both (i should keep columns have variation in values)

i used code 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)] 

thanks help

if understand correctly request, can try:

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

it looks columns more 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 

Comments