[R] deleting row when any col is.na

Douglas Bates bates at stat.wisc.edu
Sun Feb 11 14:31:06 CET 2007


On 2/11/07, Milton Cezar Ribeiro <milton_ruser at yahoo.com.br> wrote:

> How can I delete rows from a data.frame where almost one column is.na()?

The na.omit function does this.

> set.seed(123454321)
> x <- matrix(rnorm(50, mean = 1), ncol = 5)
> x[x < 0] <- NA
> df <- data.frame(x)
> df
           X1         X2        X3        X4        X5
1          NA         NA        NA 2.1072043 2.5623080
2  2.42544607 1.41903027 0.9652384 0.8869558 0.6824612
3  0.09657245         NA 1.6891345        NA 0.9867981
4  0.36620256 0.20111707        NA 0.6959298 0.3167434
5  2.73609165 0.07639585        NA 0.6348793 2.3797860
6          NA 0.43590076        NA 1.6082227 0.6580598
7  2.11889699 0.23726675 0.5066854 0.8967605 1.7082769
8  2.77162997 1.24540928        NA 0.4278059 1.1767292
9  0.55998073 1.80824613 0.6645171 1.7956458 2.1733173
10         NA 0.41624884 0.6055874 0.4814155 0.6728243
> na.omit(df)
         X1        X2        X3        X4        X5
2 2.4254461 1.4190303 0.9652384 0.8869558 0.6824612
7 2.1188970 0.2372667 0.5066854 0.8967605 1.7082769
9 0.5599807 1.8082461 0.6645171 1.7956458 2.1733173



More information about the R-help mailing list