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:> af 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> 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
Xiao Shi wrote on 5/6/2005 6:21 AM:> 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 >How about: a[rowSums(is.na(a)) < 2, ] HTH, --sundar
one way to do it is: mat <- matrix(rnorm(20*20), 20, 20) mat[sample(400, 40)] <- NA ##### mat mat[rowSums(is.na(mat)) <= 2, ] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Xiao Shi" <bioconductor.cn at gmail.com> To: <r-help at stat.math.ethz.ch> Sent: Friday, May 06, 2005 3:21 PM Subject: [R] how to get such a subset of a matrix?> 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 >
Try: a[rowSums(is.na(a)) <= 2, ] Andy> From: Xiao Shi > > 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 > > >
Seemingly Similar Threads
- Is there a function in R can help me to plot such a figure?
- proportional matrix rows
- do.call("+", ...)
- How to get the remaining vector after sampling a subset?
- How to plot a matrix with 18 rows by row vs. a vector in a single graph, resulting 18 lines with different colors?