Good day R-community, i just wondered if it is a bug or a feature... When i have a matrix "mat" with one column and i delete the last row with mat <- mat[-nrow(mat),] the result is a list. So my next call mat[10,] will throw an "wrong dimension" error. The proper call must be: mat <- as.matrix(mat[-nrow(mat),]) So is this desired behavior or a bug? I use R-version 2.15.3, but reconstructed this behavior in 3.2.0 as well. greetings -- *Joscha Zander* Roche Diagnostics GmbH DXRDDD..6164 Sandhofer Strasse 116 68305 Mannheim / Germany mailto:joscha.zander at roche.com <joscha.zander at roche.com> *Roche Diagnostics GmbH* Sandhofer Stra?e 116; D?68305 Mannheim; Telefon +49?621?759?0; Telefax +49?621?759?2890 Sitz der Gesellschaft: Mannheim - Registergericht: AG Mannheim HRB 3962 - Gesch?ftsf?hrung: Dr. Ursula Redeker, Sprecherin; Edgar Vieth - Aufsichtsratsvorsitzender: Dr. Severin Schwan *Confidentiality Note* This message is intended only for the use of the named recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient, please contact the sender and delete the message. Any unauthorized use of the information contained in this message is prohibited. [[alternative HTML version deleted]]
> On Jul 3, 2015, at 9:33 AM, Zander, Joscha <joscha.zander at roche.com> wrote: > > Good day R-community, > > i just wondered if it is a bug or a feature... > > When i have a matrix "mat" with one column and i delete the last row with > > mat <- mat[-nrow(mat),] the result is a list. > > So my next call mat[10,] will throw an "wrong dimension" error. > The proper call must be: > > mat <- as.matrix(mat[-nrow(mat),]) > > So is this desired behavior or a bug? > > I use R-version 2.15.3, but reconstructed this behavior in 3.2.0 as well. > > greetings > > -- > *Joscha Zander*See: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-my-matrices-lose-dimensions_003f mat <- matrix(1:12, ncol = 1)> str(mat)int [1:12, 1] 1 2 3 4 5 6 7 8 9 10 ...> mat[-nrow(mat), ][1] 1 2 3 4 5 6 7 8 9 10 11 # This is a vector, not a list> str(mat[-nrow(mat), ])int [1:11] 1 2 3 4 5 6 7 8 9 10 ?> mat[-nrow(mat), , drop = FALSE][,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 [10,] 10 [11,] 11 # This is a matrix> str(mat[-nrow(mat), , drop = FALSE])int [1:11, 1] 1 2 3 4 5 6 7 8 9 10 ? Regards, Marc Schwartz P.S. are you restricted in being able to upgrade from a version of R that is two years old?
Hi, On Fri, Jul 3, 2015 at 10:33 AM, Zander, Joscha <joscha.zander at roche.com> wrote:> Good day R-community, > > i just wondered if it is a bug or a feature... > > When i have a matrix "mat" with one column and i delete the last row with > > mat <- mat[-nrow(mat),] the result is a list.I have no idea how you're getting a list from a matrix (see below). Perhaps you mean a data frame?> So my next call mat[10,] will throw an "wrong dimension" error. > The proper call must be: > > mat <- as.matrix(mat[-nrow(mat),]) > > So is this desired behavior or a bug?If you check ?"[" you'll see the drop argument, which is what I guess you want. Compare:> mat <- matrix(1:6, nrow=2) > mat <- mat[-nrow(mat), ] > class(mat) # not a list[1] "integer"> dim(mat)NULL> is.list(mat) # see? really not a list[1] FALSE> mat[1] 1 3 5> mat <- matrix(1:6, nrow=2) > mat <- mat[-nrow(mat), , drop=FALSE] > class(mat)[1] "matrix"> dim(mat)[1] 1 3> mat[,1] [,2] [,3] [1,] 1 3 5> I use R-version 2.15.3, but reconstructed this behavior in 3.2.0 as well. > > greetings > > -- > *Joscha Zander* >--Sarah Goslee http://www.functionaldiversity.org [[alternative HTML version deleted]]
On 04/07/15 03:43, Sarah Goslee wrote:> Hi, > > On Fri, Jul 3, 2015 at 10:33 AM, Zander, Joscha <joscha.zander at roche.com> > wrote: >> Good day R-community, >> >> i just wondered if it is a bug or a feature... >> >> When i have a matrix "mat" with one column and i delete the last row with >> >> mat <- mat[-nrow(mat),] the result is a list. > > I have no idea how you're getting a list from a matrix (see below). Perhaps > you mean a data frame?<SNIP> No. The Zander person just means a vector. Psigh. Note to the Zander person: It is important in R programming to get your concepts straight. It is also important in communicating with others to get your terminology straight. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276