R套用错误-as.matrix.data.frame()中的错误 [英] R apply error - error in as.matrix.data.frame()

查看:1156
本文介绍了R套用错误-as.matrix.data.frame()中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个莫名其妙的错误. 我正在使用以下功能删除任何列中包含NA观测值的数据框的行

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

现在我有一个包含约2993行的数据框.当我通过函数传递此数据帧时,遇到以下错误:

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]

感谢您的答复,

推荐答案

解决问题的另一种方法是na.omit

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

这篇关于R套用错误-as.matrix.data.frame()中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆