[R] how to get such a subset of a matrix?

Robin Hankin r.hankin at noc.soton.ac.uk
Fri May 6 15:31:45 CEST 2005



R> a <- matrix(1:36,6,6)
R> is.na(a[row(a)<col(a)]) <- TRUE
R> a
      [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1   NA   NA   NA   NA   NA
[2,]    2    8   NA   NA   NA   NA
[3,]    3    9   15   NA   NA   NA
[4,]    4   10   16   22   NA   NA
[5,]    5   11   17   23   29   NA
[6,]    6   12   18   24   30   36

 > a[apply(a,1,function(x){sum(is.na(x))<=2}),]
      [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    4   10   16   22   NA   NA
[2,]    5   11   17   23   29   NA
[3,]    6   12   18   24   30   36
 >

Hope this helps

rksh

On May 6, 2005, at 02:21 pm, Xiao Shi wrote:

> Hi everybody,
> Maybe this question is quite simple but i just don't know how to make 
> it.
> I have a matrix a somewhat like this one but bigger:
>> a
> f g h i j k
> a NA NA 11 16 21 26
> b NA NA 12 17 22 27
> c NA 8 13 18 23 28
> d NA 9 14 19 24 29
> e NA 10 15 20 25 30
> And i want to get the rows which at most have 2 Na.
> Thanks in advance.
> Shi Jiantao
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>
>
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743




More information about the R-help mailing list