例: 統(tǒng)計數(shù)據(jù)框內(nèi)某列內(nèi)NA在所有行中出現(xiàn)的次數(shù)
- 創(chuàng)建數(shù)據(jù)框
data <- data.frame(A=c(1,2,3,NA,NA,4,5,NA),B=c(11,NA,34,NA,46,7,9,NA),
C=c(23,NA,2,5,8,NA,8,NA),D=c(4,5,7,8,9,24,56,6))
data
- 統(tǒng)計函數(shù)
data_sum <- function(x){
times <- length(which(is.na(data[,x])))
times_data <- data.frame(
'ID' <- x,
'Times' <- times
)
return(times_data)
}
- 數(shù)據(jù)輸入到函數(shù)內(nèi)(用lappy)
num_data <- c(1:4) # 共四列
sum_data_NA <- lapply(num_data, data_sum)
sum_data_NA <- ldply(sum_data_NA,data.frame) # ldply將其轉(zhuǎn)換成數(shù)據(jù)框
sum_data_NA