[R] basics on dataframes

jim holtman jholtman at gmail.com
Sun Apr 6 03:24:57 CEST 2008


You must add a check for the NAs:

> a[(a$V1 > 1) & (!is.na(a$V1)),]
   V1        V2        V3       V4       V5
7 100 -2.343761 -132.6063 101.7575 1.187004



On Sat, Apr 5, 2008 at 8:05 PM, Georg Ehret <georgehret at gmail.com> wrote:
> Dear R community,
>    I am using a dataframe with lots of NAs and wish to create subsets from
> that dataframe. In the example below I intend to create a subset with all
> entries with V1>1. To my surprise the result also contains all entries with
> V1=NA. I would wish to learn more why this is or what I do wrong. I know
> that I can use the command "subset" and the NAs will not appear, but what is
> "wrong" with a[a$V1>1,]?
> Thank you!
> Georg.
> *******************
> Georg Ehret
> Johns Hopkins
> Baltimore, USA
>
>
> > a
>            V1        V2          V3         V4         V5
> 1           NA        NA          NA -2056.5981  0.8472496
> 2           NA        NA          NA  1407.8786  1.3349255
> 3           NA        NA          NA   579.2203  0.6082337
> 4           NA        NA          NA  1337.0740  1.0346453
> 5           NA        NA   -3.732253  -246.7270  1.9218894
> 6           NA -4.111501  -50.372053 -1512.2618 -0.8425137
> 7  100.0000000 -2.343761 -132.606282   101.7575  1.1870040
> 8   -1.8899018 11.921323   15.988519 -1199.5994  0.1907434
> 9    0.4819706  5.677561   29.677330  -200.9182 -0.1213408
> 10  -1.4981107  6.224487  209.133480   188.8852 -1.3321220
> > b<-a[a$V1>1,]
> > b
>      V1        V2        V3       V4       V5
> NA    NA        NA        NA       NA       NA
> NA.1  NA        NA        NA       NA       NA
> NA.2  NA        NA        NA       NA       NA
> NA.3  NA        NA        NA       NA       NA
> NA.4  NA        NA        NA       NA       NA
> NA.5  NA        NA        NA       NA       NA
> 7    100 -2.343761 -132.6063 101.7575 1.187004
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list