matrix -> delete last row -> list

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

matrix -> delete last row -> list

Zander, Joscha
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:[hidden email] <[hidden email]>

 *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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: matrix -> delete last row -> list

Marc Schwartz-3

> On Jul 3, 2015, at 9:33 AM, Zander, Joscha <[hidden email]> 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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: matrix -> delete last row -> list

Sarah Goslee
In reply to this post by Zander, Joscha
Hi,

On Fri, Jul 3, 2015 at 10:33 AM, Zander, Joscha <[hidden email]>
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: matrix -> delete last row -> list

Rolf Turner
On 04/07/15 03:43, Sarah Goslee wrote:

> Hi,
>
> On Fri, Jul 3, 2015 at 10:33 AM, Zander, Joscha <[hidden email]>
> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: matrix -> delete last row -> list

Michael Sumner-2
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 <[hidden email]> wrote:

> On 04/07/15 03:43, Sarah Goslee wrote:
>>
>> Hi,
>>
>> On Fri, Jul 3, 2015 at 10:33 AM, Zander, Joscha <[hidden email]>
>> 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
>
> ______________________________________________
> [hidden email] 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.
>

--
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: [hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.