Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

r - Find names of columns which contain missing values

I want to find all the names of columns with NA or missing data and store these column names in a vector.

# create matrix
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
mymatrix
#      aa bb cc dd ee
# [1,]  1  2  3  4  5
# [2,] NA  7  8  9 10
# [3,] NA 12 13 14 NA
# [4,] 16 17 18 19 20

The desired result: columns "aa" and "ee".

My attempt:

bad <- character()
for (j in 1:4){     
  tmp <- which(colnames(mymatrix[j, ]) %in% c("", "NA"))
  bad <- tmp
}

However, I keep getting integer(0) as my output. Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Like this?

colnames(mymatrix)[colSums(is.na(mymatrix)) > 0]
# [1] "aa" "ee"

Or as suggested by @thelatemail:

names(which(colSums(is.na(mymatrix)) > 0))
# [1] "aa" "ee"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...