Skip to content

matrix -> delete last row -> list

5 messages · Zander, Joscha, Marc Schwartz, Sarah Goslee +2 more

#
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
#
See:

  http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-my-matrices-lose-dimensions_003f


mat <- matrix(1:12, ncol = 1)
int [1:12, 1] 1 2 3 4 5 6 7 8 9 10 ...
[1]  1  2  3  4  5  6  7  8  9 10 11

# This is a vector, not a list
int [1:11] 1 2 3 4 5 6 7 8 9 10 ?
[,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
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:
I have no idea how you're getting a list from a matrix (see below). Perhaps
you mean a data frame?
If you check
?"["
you'll see the drop argument, which is what I guess you want. Compare:
[1] "integer"
NULL
[1] FALSE
[1] 1 3 5
[1] "matrix"
[1] 1 3
[,1] [,2] [,3]
[1,]    1    3    5
Sarah Goslee
http://www.functionaldiversity.org
#
On 04/07/15 03:43, Sarah Goslee wrote:
<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
#
Well 'list' in R is pretty naturally the same as R's 'atomic vector' in
scads of languages. In R the term needs special care since it's still a
'vector'.

'Degenerate dimension' is probably a helpful phrase for understanding what
is happening here.

Cheers, Mike
On Saturday, July 4, 2015, Rolf Turner <r.turner at auckland.ac.nz> wrote:
with
Perhaps
concepts straight.  It is also important in communicating with others to
get your terminology straight.
http://www.R-project.org/posting-guide.html