[R] Problem Subsetting Rows that Have NA's

Ben Tupper btupper at bigelow.org
Tue Oct 24 22:12:47 CEST 2017


Hi,

It's related to how NAs are treated in comparison operations.  See the Details section of https://www.rdocumentation.org/packages/base/versions/3.4.1/topics/Comparison <https://www.rdocumentation.org/packages/base/versions/3.4.1/topics/Comparison>

You can try something like this...

x[which(x[,2] %in% 0),]
#      [,1] [,2]
# [1,]    4    0
# [2,]    5    0


... but I'm not sure if it is bullet proof.  Others may have more insight.

Cheers,
Ben



> On Oct 24, 2017, at 3:05 PM, BooBoo <booboo at gforcecable.com> wrote:
> 
> This has every appearance of being a bug. If it is not a bug, can someone tell me what I am asking for when I ask for "x[x[,2]==0,]". Thanks.
> 
> > #here is the toy dataset
> > x <- rbind(c(1,1),c(2,2),c(3,3),c(4,0),c(5,0),c(6,NA),
> +   c(7,NA),c(8,NA),c(9,NA),c(10,NA)
> + )
> > x
>      [,1] [,2]
> [1,]    1    1
> [2,]    2    2
> [3,]    3    3
> [4,]    4    0
> [5,]    5    0
> [6,]    6   NA
> [7,]    7   NA
> [8,]    8   NA
> [9,]    9   NA
> [10,]   10   NA
> >
> > #it contains rows that have NA's
> > x[is.na(x[,2]),]
>     [,1] [,2]
> [1,]    6   NA
> [2,]    7   NA
> [3,]    8   NA
> [4,]    9   NA
> [5,]   10   NA
> >
> > #seems like an unreasonable answer to a reasonable question
> > x[x[,2]==0,]
>     [,1] [,2]
> [1,]    4    0
> [2,]    5    0
> [3,]   NA   NA
> [4,]   NA   NA
> [5,]   NA   NA
> [6,]   NA   NA
> [7,]   NA   NA
> >
> > #this is more what I was expecting
> > x[which(x[,2]==0),]
>     [,1] [,2]
> [1,]    4    0
> [2,]    5    0
> >
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecocast Reports: http://seascapemodeling.org/ecocast.html
Tick Reports: https://report.bigelow.org/tick/
Jellyfish Reports: https://jellyfish.bigelow.org/jellyfish/




	[[alternative HTML version deleted]]



More information about the R-help mailing list