【发布时间】:2012-07-18 06:47:14
【问题描述】:
我遇到了一个莫名其妙的错误。 我正在使用以下函数删除任何列中包含 NA 观察的数据帧行
##### removes NA'd rows from a dataFrame
wipeNArows<-function(X){
rowsToDelete<-unique(unlist(apply(apply(X,2,is.na),2,which)))
if (length(rowsToDelete)>0){
return (X[-rowsToDelete,])
}
else{
return (X)
}
}
这个函数可以正常工作,例如一个可重现的例子是:
testFrame<-data.frame(x=rpois(20,10),y=rpois(20,10),z=rpois(20,10))
rowsToDelete<-sample(1:nrow(testFrame),5,FALSE)
testFrame$x[rowsToDelete]<-NA
testFrame
wipeNArows(testFrame) ### removes the rows where NA is encountered
现在我有一个包含大约 2993 行的数据框。当我通过函数传递这个数据框时,我面临以下错误:
Error in apply(apply(X, 2, is.na), 2, which) :
error in evaluating the argument 'X' in selecting a method for function 'apply': Error in as.matrix.data.frame(X) :
dims [product 14965] do not match the length of object [14974]
感谢您的回复,
【问题讨论】:
-
你能提供一个例子,它不工作,而不是一个工作?
-
我建议在函数的开头插入
browser()。这样,您就可以单步执行您的代码,检查每个元素并找出错误。