How do I judge if a matrix contain any NA or otherwise non-missing, non-numerical? In the following, I would like to deliver ONE logical of TRUE or FALSE, rather than a 4 x 4 matrix containing TRUE or FALSE. Thank you. > a<-matrix(1:16,nrow=4) > diag(a)<-NA > a [,1] [,2] [,3] [,4] [1,] NA 5 9 13 [2,] 2 NA 10 14 [3,] 3 7 NA 15 [4,] 4 8 12 NA > is.na(a) [,1] [,2] [,3] [,4] [1,] TRUE FALSE FALSE FALSE [2,] FALSE TRUE FALSE FALSE [3,] FALSE FALSE TRUE FALSE [4,] FALSE FALSE FALSE TRUE
> all(is.na(a)) [1] FALSE------------------ Original ------------------ From: "Steven Yen";<syen04 at gmail.com>; Date: Mon, Jul 27, 2015 09:10 AM To: "r-help mailing list"<r-help at r-project.org>; Subject: [R] Judging if a matrix contains any NA How do I judge if a matrix contain any NA or otherwise non-missing, non-numerical? In the following, I would like to deliver ONE logical of TRUE or FALSE, rather than a 4 x 4 matrix containing TRUE or FALSE. Thank you. > a<-matrix(1:16,nrow=4) > diag(a)<-NA > a [,1] [,2] [,3] [,4] [1,] NA 5 9 13 [2,] 2 NA 10 14 [3,] 3 7 NA 15 [4,] 4 8 12 NA > is.na(a) [,1] [,2] [,3] [,4] [1,] TRUE FALSE FALSE FALSE [2,] FALSE TRUE FALSE FALSE [3,] FALSE FALSE TRUE FALSE [4,] FALSE FALSE FALSE TRUE ______________________________________________ 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. [[alternative HTML version deleted]]
On Jul 26, 2015, at 6:10 PM, Steven Yen wrote:> How do I judge if a matrix contain any NA or otherwise non-missing, non-numerical? > In the following, I would like to deliver ONE logical of TRUE or FALSE, rather than a 4 x 4 matrix containing TRUE or FALSE. Thank you. > > > a<-matrix(1:16,nrow=4) > > diag(a)<-NA > > a > [,1] [,2] [,3] [,4] > [1,] NA 5 9 13 > [2,] 2 NA 10 14 > [3,] 3 7 NA 15 > [4,] 4 8 12 NA> any(is.na( a))[1] TRUE> > is.na(a) > [,1] [,2] [,3] [,4] > [1,] TRUE FALSE FALSE FALSE > [2,] FALSE TRUE FALSE FALSE > [3,] FALSE FALSE TRUE FALSE > [4,] FALSE FALSE FALSE TRUE > > ______________________________________________ > 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.David Winsemius Alameda, CA, USA
On 26/07/2015 9:10 PM, Steven Yen wrote:> How do I judge if a matrix contain any NA or otherwise non-missing, > non-numerical?David told you about any(). You may also want to use !is.finite() instead of is.na(). Duncan Murdoch> In the following, I would like to deliver ONE logical of TRUE or FALSE, > rather than a 4 x 4 matrix containing TRUE or FALSE. Thank you. > > > a<-matrix(1:16,nrow=4) > > diag(a)<-NA > > a > [,1] [,2] [,3] [,4] > [1,] NA 5 9 13 > [2,] 2 NA 10 14 > [3,] 3 7 NA 15 > [4,] 4 8 12 NA > > is.na(a) > [,1] [,2] [,3] [,4] > [1,] TRUE FALSE FALSE FALSE > [2,] FALSE TRUE FALSE FALSE > [3,] FALSE FALSE TRUE FALSE > [4,] FALSE FALSE FALSE TRUE > > ______________________________________________ > 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. >
> On 27 Jul 2015, at 03:18 , LiuNing <liuning.1982 at qq.com> wrote: > >> all(is.na(a)) [1] FALSE > >Bzzt! Try !all(is.finite(a)) -pd> > ------------------ Original ------------------ > From: "Steven Yen";<syen04 at gmail.com>; > Date: Mon, Jul 27, 2015 09:10 AM > To: "r-help mailing list"<r-help at r-project.org>; > > Subject: [R] Judging if a matrix contains any NA > > > > How do I judge if a matrix contain any NA or otherwise non-missing, > non-numerical? > In the following, I would like to deliver ONE logical of TRUE or FALSE, > rather than a 4 x 4 matrix containing TRUE or FALSE. Thank you. > >> a<-matrix(1:16,nrow=4) >> diag(a)<-NA >> a > [,1] [,2] [,3] [,4] > [1,] NA 5 9 13 > [2,] 2 NA 10 14 > [3,] 3 7 NA 15 > [4,] 4 8 12 NA >> is.na(a) > [,1] [,2] [,3] [,4] > [1,] TRUE FALSE FALSE FALSE > [2,] FALSE TRUE FALSE FALSE > [3,] FALSE FALSE TRUE FALSE > [4,] FALSE FALSE FALSE TRUE > > ______________________________________________ > 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. > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com