2013-05-02 20 views
1

这里是为了说明一个例子:R中是否有变量complete.cases的类比?

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 

回答

5

或者是这样的:

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

+1简单的人都是最佳的。我会用一个'x []'来返回OP所需要的,即''[complete.cases(t(x))]'' – 2013-05-02 14:55:20

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) 
4

你可以做这样的事情:

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

这个小功能应该工作:

for (a in c(1:length(x))){ 
    ifelse(TRUE%in%is.na(x[,a]),print ('INCOMPLETE'),print ('COMPLETE')) 
} 
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 
0

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

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