对于R中的变量,是否有类似的complete.cases?

时间:2013-05-02 14:23:29

标签: r

以下是一个示例:

x = data.frame(x1=1:3, x2=2:4, x3=3:5)
x
#   x1 x2 x3
# 1  1  2  3
# 2  2  3  4
# 3  3  4  5
x[2, 1] = NA
x[3, 2] = NA
complete.cases(x)
# [1]  TRUE FALSE FALSE
x[complete.cases(x), , drop=FALSE]
#   x1 x2 x3
# 1  1  2  3

如果改为完整的情况,我想过滤完整的变量(列)?在上面的例子中,它应该是这样的:

x[,3,drop=FALSE]
#   x3
# 1  3
# 2  4
# 3  5

6 个答案:

答案 0 :(得分:5)

或类似的东西:

 x[, complete.cases(t(x)), drop=FALSE] # Tks Simon

答案 1 :(得分:4)

您可以这样做:

R> x[,sapply(x, function(v) sum(is.na(v))==0), drop=FALSE]
  x3
1  3
2  4
3  5

答案 2 :(得分:0)

我确信这里有一个更清洁的策略,但我认为以下功能也可以使用:

x = data.frame(x1=1:3, x2=2:4, x3=3:5)
x[2, 1] = NA
x[3, 2] = NA

complete.cols = function(dat){   
  non.missing.test = apply(dat,2,function(t){sum(is.na(t))==0})
  dat.complete.cols = data.frame(dat[,which(non.missing.test == TRUE)])
  names(dat.complete.cols) = names(dat)[which(non.missing.test == TRUE)]
  return(dat.complete.cols)
}

complete.cols(x)

答案 3 :(得分:0)

这个小功能应该有效:

for (a in c(1:length(x))){
    ifelse(TRUE%in%is.na(x[,a]),print ('INCOMPLETE'),print ('COMPLETE'))
}

答案 4 :(得分:0)

 complete.col <- function(col) sum(is.na(col))==0
 dfrm[ sapply(dfrm, complete.col) ]
 #or almost equivalently
 dfrm[ ,   ]

 #If you wanted the numbers of the columns with no missing
 which( sapply(dfrm, complete.col) )

 # To wrap `sapply` around the function on a single column functions
 complete.cols <- function(dfrm) sapply(dfrm, function(col) sum(is.na(col))==0)
 x[ complete.cols(x) ]
#--------
  x3
1  3
2  4
3  5

答案 5 :(得分:0)

您可以使用sapply使用该结果检查缺失值和子集的列:

x[sapply(x,function(y) !any(is.na(y)))]
  x3
1  3
2  4
3  5