从r中的数据集中提取不完整的案例

时间:2016-09-18 07:09:55

标签: r

来自这个例子:

    ## x
   ##  v1 v2 v3
   ##1 90 55 NA
   ##2 NA 45  8
   ##3 85 NA  5
   ##4 NA 33  7
   ##5 55 30  4
   ##6 60 20  3
   ##7 75 15  2
   ##8 80 23  6
     # ici is where Incomplete case indicator 

     ici=(function (x) 
     return(is.na(x)))

    ici(x)
            v1    v2    v3
  ##  [1,] FALSE FALSE  TRUE
  ##  [2,]  TRUE FALSE FALSE
  ##  [3,] FALSE  TRUE FALSE
  ##  [4,]  TRUE FALSE FALSE
  ##  [5,] FALSE FALSE FALSE
  ##  [6,] FALSE FALSE FALSE
  ##  [7,] FALSE FALSE FALSE
  ##  [8,] FALSE FALSE FALSE

     # Extracts incomplete cases from a data set

     ic=(function (x, drop) 
     return(x[ici(x)]))(x, drop)

     ic(x)
   ## Error: could not find function "ic"

从上次运行(错误:找不到函数" ic")为什么它给我这个以及如何用函数ic解决这个问题

1 个答案:

答案 0 :(得分:4)

我不太明白你的函数ic正在做什么,包括括号和drop位。

如果您需要完整案例,最方便的方式是na.omit(x)x[complete.cases(x), ]。但由于您需要不完整的案例,我们需要x[!complete.cases(x), ]

我当然明白ici正在做什么。它只是is.na。如果我被要求从is.na开始,我将使用以下内容:

x[rowSums(is.na(x)) > 0L, ]