lever lever - 1 year ago 57
R Question

Find names of columns which contain missing values

I want to find all the names of columns with

or missing data and store these column names in a vector. This is my latest attempt:

a <- c(1,2,3,4,5,NA,7,8,9,10,NA,12,13,14,NA,16,17,18,19,20)
cnames <- c("aa", "bb","cc","dd","ee")
mymatrix <- matrix(a, nrow=4, ncol=5, byrow=TRUE,)
colnames(mymatrix) <- cnames
bad <- character()
for (j in 1:4){
tmp <- which(colnames(mymatrix[j,])%in%c("","NA"))
bad <- tmp

I keep getting
as my ouput when I'm expecting "aa" "ee". Any help is appreciated.

Answer Source

Like this?

colnames(mymatrix)[colSums(is.na(mymatrix)) > 0]
# [1] "aa" "ee"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download