English 中文(简体)
留级职能: 1 北美值(原产地)
原标题:Function to retain rows with >= 1 NA value (opposite of na.omit)
  • 时间:2011-08-02 15:07:05
  •  标签:
  • r

是否有一项职能,至少保留一个新国家,如果没有任何新国家......在<代码>na.omit(对面的,则将抛弃物。 我尝试了<条码>!na.omit(),但那是 t。

最佳回答

Use the negation of complete.cases, i.e. !complete.cases(x)

改编自?complete.cases:

data(airquality)
head(airquality[!complete.cases(airquality), ])

   Ozone Solar.R Wind Temp Month Day
5     NA      NA 14.3   56     5   5
6     28      NA 14.9   66     5   6
10    NA     194  8.6   69     5  10
11     7      NA  6.9   74     5  11
25    NA      66 16.6   57     5  25
26    NA     266 14.9   58     5  26
问题回答

这里是将 du数据用于矩阵的一种方法,但可以很容易地适应数据框架情况。

mat <- matrix(runif(100), ncol = 10)
set.seed(2)
mat[sample(100, 10)] <- NA

我们可以使用<条码>is.na(),根据<条码>将矩阵转换成一个逻辑格式。 NA出席:

> is.na(mat)
       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
 [1,] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE
 [2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [3,] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [4,] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
 [5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [6,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [7,] FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
 [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
 [9,] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[10,] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE

From that we can apply() the any() function to the rows, to return a logical index for rows with one or more NAs:

> apply(is.na(mat), 1, any)
 [1]  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE

共同实现这一目标:

ind <- apply(is.na(mat), 1, any)
mat[ind, ]

提供:

> ind <- apply(is.na(mat), 1, any)
> mat[ind, ]
          [,1]       [,2]      [,3]        [,4]       [,5]       [,6]
[1,] 0.6618988 0.01041453 0.9817279 0.007109038 0.77002786         NA
[2,] 0.8368892         NA 0.1150841 0.683403423 0.62512173 0.04217553
[3,] 0.1505014 0.86886104 0.1632009 0.929720222         NA 0.18467346
[4,] 0.1492469         NA 0.9746879 0.785878913 0.38814476         NA
[5,] 0.3570626 0.28487057 0.3490884 0.988902156 0.46150111 0.86784466
[6,] 0.9626440         NA 0.5019699 0.613952910 0.21867519 0.40264274
[7,] 0.1323720 0.15046975 0.8103973 0.710185730 0.06593551 0.57268500
           [,7]      [,8]      [,9]     [,10]
[1,] 0.35064257 0.9767552 0.2009347        NA
[2,] 0.02505036 0.3799989 0.9806000 0.3733586
[3,] 0.40110104 0.5603876 0.8289221 0.5743769
[4,] 0.97151543 0.4269434 0.8989719 0.8726963
[5,] 0.32372244        NA 0.4533770 0.1105549
[6,] 0.73319143 0.1153091 0.1474178 0.9527002
[7,]         NA 0.4400317        NA 0.5690021

简单的在线选择

is.na() returns a matrix of booleans detecting NA values (same dimensions as input data frame)

<代码>rowSums(s number of TRUE Value perrow

最后,在发现至少1个国民时,使用本地的留级。

data[rowSums(is.na(data)) > 0, ]




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签