R apply error - error in as.matrix.data.frame()

2019-03-01 14:43发布

问题:

i am encountering a baffling error. i am using the following function to delete rows of a dataframe containing an NA observation in any column

##### 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)
  }
}

This function works fine normally, for instance a reproducible example is:

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

Now i have a data frame containing about 2993 rows. When i pass this data frame through the function i face the following error:

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]

Thanks for responses,

回答1:

Another way to solve your problem would be na.omit

na.omit(testFrame)

    x  y  z
2   7 11 11
3  12 10 10
4  13 10  9
6  11 10 12
7  13 14  8
8   7  9  7
9   8 11 12
10  5 10  7
11  5 15  9
12  7 13  9
15 15  8  9
16 13  7 15
17  5 10 12
18  9  8  6
20 18  7  6


回答2:

Works fine for me, but why not use ?complete.cases

testFrame[complete.cases(testFrame),]
    x  y  z
2  10  8 13
3  11 16 18
4  11  7  7
6   8  8 14
7   9 11 11
8  12 11  5
9  10  7  4
10  7 12  9
11 10 13 11
12  9 12 10
13 10  5  8
14 13  5  8
15 11  5  5
18 13 14  7
19  2 13  8

identical(testFrame[complete.cases(testFrame),], wipeNArows(testFrame))
[1] TRUE


回答3:

hmm thanks for replies, wasn't aware of the complete.cases function. but that gives another error

 Error in complete.cases(dFrame) : not all arguments have the same length

chisq.test Error Message --> appears to address this issue in a way.

the issue with the problematic data frame is that it contained a POSIXlt object column with dates. clearly complete.cases and apply internal workings aren't handling this too well. the workaround is to cast to character with strftime and then back with strptime.

thanks,



回答4:

General case, if you do not have na's in your data, then as Aditya Sihag suggested, the problem could be one of your data.frame columns's datatype may be a list of objects such as a list or POSIXlt object. You can either cast them or you can just use lapply on the column alone. But again make sure your column datatype is not a list or POSIXlt before applying lapply and if yes, then just cast it.



回答5:

Without the problem data, I can only suggest a different function

wipe_na_rows <- function(X){
  X[!apply(X, 1, function(x) any(is.na(x))),]
}


标签: r apply