ryszard.czerminski at novartis.com
2006-Apr-07 13:16 UTC
[R] strange matrix behaviour: is there a matrix with one row?
Consider this:> y <- matrix(1:8, ncol=2) > is.matrix(y[-c(1,2),])[1] TRUE> is.matrix(y[-c(1,2,3),])[1] FALSE> is.matrix(y[-c(1,2,3,4),])[1] TRUE It seems like an inconsistent behaviour: - with 2 or more rows we have a matrix - with 1 row we do not have a matrix and - with 0 rows we have a matrix again I just stumbled on this behaviour, because I had a problem with my program in which I have assumed that matrix with some rows removed is still a matrix, which seems to be mostly true, but it is not true if only one row is left. Comments? Suggestions? How to work around this problem - without to many "if" statements? Best regards, Ryszard
François Pinard
2006-Apr-07 13:27 UTC
[R] strange matrix behaviour: is there a matrix with one row?
[ryszard.czerminski at novartis.com]>> y <- matrix(1:8, ncol=2) >> is.matrix(y[-c(1,2),]) >[1] TRUE >> is.matrix(y[-c(1,2,3),]) >[1] FALSE >> is.matrix(y[-c(1,2,3,4),]) >[1] TRUE>It seems like an inconsistent behaviour: >- with 2 or more rows we have a matrix >- with 1 row we do not have a matrix and >- with 0 rows we have a matrix again?'[' explains it. Using your example:> is.matrix(y[-c(1, 2), , drop=FALSE])[1] TRUE> is.matrix(y[-c(1, 2, 3), , drop=FALSE])[1] TRUE> is.matrix(y[-c(1, 2, 3, 4), , drop=FALSE])[1] TRUE -- Fran?ois Pinard http://pinard.progiciels-bpi.ca
Dimitris Rizopoulos
2006-Apr-07 13:30 UTC
[R] strange matrix behaviour: is there a matrix with one row?
this is documented: check ?"[". You need to specify drop = FALSE, i.e., is.matrix(y[-c(1,2,3), , drop = FALSE]) Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: <ryszard.czerminski at novartis.com> To: "Gabor Grothendieck" <ggrothendieck at gmail.com> Cc: "Brian Quinif" <bquinif at gmail.com>; <r-help at stat.math.ethz.ch>; <r-help-bounces at stat.math.ethz.ch> Sent: Friday, April 07, 2006 3:16 PM Subject: [R] strange matrix behaviour: is there a matrix with one row?> Consider this: > >> y <- matrix(1:8, ncol=2) >> is.matrix(y[-c(1,2),]) > [1] TRUE >> is.matrix(y[-c(1,2,3),]) > [1] FALSE >> is.matrix(y[-c(1,2,3,4),]) > [1] TRUE > > It seems like an inconsistent behaviour: > - with 2 or more rows we have a matrix > - with 1 row we do not have a matrix and > - with 0 rows we have a matrix again > > I just stumbled on this behaviour, because I had a problem > with my program in which I have assumed that matrix with > some rows removed is still a matrix, which seems to be mostly > true, but it is not true if only one row is left. > > Comments? Suggestions? > How to work around this problem - without to many "if" statements? > > Best regards, > Ryszard > > ______________________________________________ > 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 >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Wiener, Matthew
2006-Apr-07 13:31 UTC
[R] strange matrix behaviour: is there a matrix with one row?
Use drop = FALSE in your subscripting calls. That will retain "matrixness". For example: y <- matrix(1:8, ncol = 2) is.matrix(y[-c(1,2,3),,drop = FALSE] More info is on the help page for "[". You can type: ?"[" to get it from the command line. Hope this helps, Matt Wiener -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of ryszard.czerminski at novartis.com Sent: Friday, April 07, 2006 9:16 AM To: Gabor Grothendieck Cc: Brian Quinif; r-help at stat.math.ethz.ch; r-help-bounces at stat.math.ethz.ch Subject: [R] strange matrix behaviour: is there a matrix with one row? Consider this:> y <- matrix(1:8, ncol=2) > is.matrix(y[-c(1,2),])[1] TRUE> is.matrix(y[-c(1,2,3),])[1] FALSE> is.matrix(y[-c(1,2,3,4),])[1] TRUE It seems like an inconsistent behaviour: - with 2 or more rows we have a matrix - with 1 row we do not have a matrix and - with 0 rows we have a matrix again I just stumbled on this behaviour, because I had a problem with my program in which I have assumed that matrix with some rows removed is still a matrix, which seems to be mostly true, but it is not true if only one row is left. Comments? Suggestions? How to work around this problem - without to many "if" statements? Best regards, Ryszard ______________________________________________ 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