我遇到了一個莫名其妙的錯誤。 我使用下面的函數來刪除包含在任一列R應用錯誤 - as.matrix.data.frame()中的錯誤
##### 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)
}
}
該功能能正常工作正常的NA觀察的數據幀的行,例如一個可再現的例子是:
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]
感謝響應,
你可以提供一個例子,它*不*工作,而不是*工作*? – A5C1D2H2I1M1N2O1R2T1 2012-07-18 06:54:52
我建議在函數的開頭插入'browser()'。這樣,你可以遍歷你的代碼,檢查每個元素並追蹤錯誤。 – 2012-07-18 08:19:58