[Rd] subsetting a matrix by conditions (PR#9082)

ripley at stats.ox.ac.uk ripley at stats.ox.ac.uk
Mon Jul 17 08:16:25 CEST 2006


On Mon, 17 Jul 2006, zhuang at moho.ess.ucla.edu wrote:

> Full_Name: Jiancang Zhuang
> Version: 2.3.0
> OS: Windows XP, BSD, Linux
> Submission from: (NULL) (130.195.2.100)
> 
> 
> > a<-matrix(scan(), ncol=3, byrow=T)
> 1: 6 NA 4 
> 4: 5 3 6
> 7: 8 2 9 
> 10: 3 6 NA
> 13: 
> Read 12 items
> > a
>      [,1] [,2] [,3]
> [1,]    6   NA    4
> [2,]    5    3    6
> [3,]    8    2    9
> [4,]    3    6   NA
> > a[a[,2]>2,]
>      [,1] [,2] [,3]
> [1,]   NA   NA   NA
> [2,]    5    3    6
> [3,]    3    6   NA
> 
> We may hope the first row of A removed or keep the same. However, R keeps the
> first row and changes all the value in that row into NA.

What `we may hope' is not relevant to a bug report.  R works as 
documented.

In your example a[,2] is c(NA, 3, 2, 6) and so the condition is
c(NA, TRUE, FALSE, TRUE).  You do not know if the first row should be 
included or not (the information is missing), and so the correct answer is 
NA.

One reference is the R Language Definitions (R-lang) which says

Indexing with a missing (i.e.@: @code{NA}) value give an @code{NA}
result.  This rule applies also to the case of logical indexing,
i.e.@: the elements of @code{x} that have an @code{NA} selector in
@code{i} get included in the result, but their value will be @code{NA}.

Please do study the R FAQ and not misuse R-bugs to report documented 
behaviour.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list