[R] Confusing concept of vector and matrix in R

5 views
Skip to first unread message

yehengxin

unread,
Mar 29, 2010, 7:13:22 PM3/29/10
to r-h...@r-project.org

Why does R need the concept of "Vector"? In my opinion, it is a useless and
confusing concept. A vector is simply a special case of a matrix whose row
or column number is equal to 1. When I take submatrix from one matrix and
if row or column number is 1, R will automatically convert it into a vector.
It is very straightforward that a submatrix of a matrix should be a matrix.
In each time, I have to use as.matrix() to convert the vector back to
matrix. It is very annoying!
--
View this message in context: http://n4.nabble.com/Confusing-concept-of-vector-and-matrix-in-R-tp1707170p1707170.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@r-project.org mailing list
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.

Rolf Turner

unread,
Mar 29, 2010, 10:42:05 PM3/29/10
to yehengxin, r-h...@r-project.org

On 30/03/2010, at 12:13 PM, yehengxin wrote:

>
> Why does R need the concept of "Vector"? In my opinion, it is a useless and
> confusing concept. A vector is simply a special case of a matrix whose row
> or column number is equal to 1. When I take submatrix from one matrix and
> if row or column number is 1, R will automatically convert it into a vector.
> It is very straightforward that a submatrix of a matrix should be a matrix.
> In each time, I have to use as.matrix() to convert the vector back to
> matrix. It is very annoying!


Well then, why don't you go away and design and build your own statistics and
data analysis language/package to replace R? You can then make whatever
design decisions you like, and you won't have to live with the design decisions
made by such silly and inept people as John Chambers and Rick Becker and their ilk.

cheers,

Rolf Turner
######################################################################
Attention:
This e-mail message is privileged and confidential. If you are not the
intended recipient please delete the message and notify the sender.
Any views or opinions presented are solely those of the author.

This e-mail has been scanned and cleared by MailMarshal
www.marshalsoftware.com
######################################################################

Sharpie

unread,
Mar 29, 2010, 10:47:20 PM3/29/10
to r-h...@r-project.org

yehengxin wrote:
>
> Why does R need the concept of "Vector"? In my opinion, it is a useless
> and confusing concept. A vector is simply a special case of a matrix
> whose row or column number is equal to 1. When I take submatrix from one
> matrix and if row or column number is 1, R will automatically convert it
> into a vector. It is very straightforward that a submatrix of a matrix
> should be a matrix. In each time, I have to use as.matrix() to convert
> the vector back to matrix. It is very annoying!
>

Except that to a computer all "matricies" and "arrays" are just vectors for
which some human arbitrarily declared something like "row break every n
entries".

And R can be told not to perform the conversion from sub-matrix to vector,
open the help page for the subset operator:

?"["

And play with the drop option.

-Charlie

-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
--
View this message in context: http://n4.nabble.com/Confusing-concept-of-vector-and-matrix-in-R-tp1707170p1735190.html

Steve Lianoglou

unread,
Mar 29, 2010, 10:47:42 PM3/29/10
to yehengxin, r-h...@r-project.org
> Why does R need the concept of "Vector"?  In my opinion, it is a useless and
> confusing concept.  A vector is simply a special case of a matrix whose row
> or column number is equal to 1.  When I take submatrix from one matrix and
> if row or column number is 1, R will automatically convert it into a vector.
> It is very straightforward that a submatrix of a matrix should be a matrix.
> In each time, I have to use as.matrix() to convert the vector back to
> matrix.    It is very annoying!

Good thing there's a way to get around this then:

R> m <- matrix(1:20, 4, 5)
R> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20

R> m[,1]
[1] 1 2 3 4

R> m[,1,drop=FALSE]
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4

R> is.matrix(m[,1,drop=FALSE])
[1] TRUE

-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
| Memorial Sloan-Kettering Cancer Center
| Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

Gabor Grothendieck

unread,
Mar 29, 2010, 10:48:32 PM3/29/10
to yehengxin, r-h...@r-project.org
Actually R looks at it the other way around. It regards a matrix as a
special case of a vector. A vector has no dimensions. A vector with
dimensions is an array. An array with two dimensions is a matrix.

Try using drop=FALSE like this:

m <- matrix(1:6, 3)
m[, 2, drop = FALSE]

Barry Rowlingson

unread,
Mar 30, 2010, 5:04:33 AM3/30/10
to Rolf Turner, r-h...@r-project.org, yehengxin
On Tue, Mar 30, 2010 at 2:42 AM, Rolf Turner <r.tu...@auckland.ac.nz> wrote:

> Well then, why don't you go away and design and build your own statistics and
> data analysis language/package to replace R?  You can then make whatever
> design decisions you like, and you won't have to live with the design decisions
> made by such silly and inept people as John Chambers and Rick Becker and their ilk.

Aah, argument by (ironic) reference to learned authority!

Even Einstein was wrong ("God does not play dice"). He was also
right, thought he was wrong, and then we've discovered he may have
been right all along (The Cosmological Constant, Dark Energy etc).

How many of us have _never_ interfaced our foreheads with the
keyboard when something breaks because we didn't put ",drop=FALSE" in
a matrix subscript?

There is no doubt that R plays fast and loose with many concepts of
type and structure that Computer Scientists would turn their nose up
at. I would love to go away and redesign it, but I'd just end up with
python. Truth is that R's statistical power is what makes it great
because of the vast wealth of CRAN, not the R language per se with its
"features" that so fluster my comp-sci friends. And many a beginner.

We work round them by bashing our heads on the keyboards, typing
",drop=FALSE", and vowing never to do it again. And writing more unit
tests.

Barry

Mario Valle

unread,
Mar 30, 2010, 6:03:42 AM3/30/10
to Barry Rowlingson, r-h...@r-project.org
Reframe the problem. Rethink why you need to keep dimensions. I never ever had to use drop.
My .02 something
mario

--
Ing. Mario Valle
Data Analysis and Visualization Group | http://www.cscs.ch/~mvalle
Swiss National Supercomputing Centre (CSCS) | Tel: +41 (91) 610.82.60
v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax: +41 (91) 610.82.82

Rolf Turner

unread,
Mar 30, 2010, 3:48:12 PM3/30/10
to Barry Rowlingson, R-help Forum

On 30/03/2010, at 10:04 PM, Barry Rowlingson wrote:

> On Tue, Mar 30, 2010 at 2:42 AM, Rolf Turner <r.tu...@auckland.ac.nz> wrote:
>
>> Well then, why don't you go away and design and build your own statistics and
>> data analysis language/package to replace R? You can then make whatever
>> design decisions you like, and you won't have to live with the design decisions
>> made by such silly and inept people as John Chambers and Rick Becker and their ilk.
>
> Aah, argument by (ironic) reference to learned authority!

Not at all. I was not arguing about the correctness of the design, decisions.
I was objecting to the churlish tone and the fatuousness of the complaint. The
design decisions, right or wrong, were taken a long time ago, and are impossible
to change now. So if you don't like 'em, there's now't to be done but go build
your own package.

That being said, learned authority is not always correct but has a strong tendency
to be so. Decisions made by learned authority may be disputed but one should be
very sure that one knows what the whuck (Maori spelling :-) ) one is talking about
before disputing. Cf. fortune("matter of opinion").

As regards features of R flustering comp-sci people: So many dysfunctional pieces
of software are designed by arrogant computer scientists who do not actually *use*
that software, do not know the background of the area of application, and do not
understand the area of application. I'll go with stats software designed by
statisticians rather than computer scientists any day. Who designed Excel?

cheers,

Rolf


######################################################################
Attention:
This e-mail message is privileged and confidential. If you are not the
intended recipient please delete the message and notify the sender.
Any views or opinions presented are solely those of the author.

This e-mail has been scanned and cleared by MailMarshal
www.marshalsoftware.com
######################################################################

______________________________________________

lith

unread,
Mar 30, 2010, 4:08:20 PM3/30/10
to r-h...@r-project.org
> Reframe the problem. Rethink why you need to keep dimensions. I never ever had to use drop.

The problem is that the type of the return value changes if you happen
to forget to use drop = FALSE, which can easily turn into a nightmare:

m <- matrix(1:20, ncol=4)
for (i in seq(3, 1, -1)) {
print(class(m[1:i, ]))
}
[1] "matrix"
[1] "matrix"
[1] "integer"

Johannes Huesing

unread,
Apr 1, 2010, 10:27:38 PM4/1/10
to r-h...@r-project.org
Rolf Turner <r.tu...@auckland.ac.nz> [Tue, Mar 30, 2010 at 09:48:12PM CEST]:
[...]
> Who designed Excel?

Among others, Joel Spolsky. Is that an appeal to authority?

<http://www.joelonsoftware.com/articles/fog0000000020.html>


--
Johannes Hüsing There is something fascinating about science.
One gets such wholesale returns of conjecture
mailto:joha...@huesing.name from such a trifling investment of fact.
http://derwisch.wikidot.com (Mark Twain, "Life on the Mississippi")

Stu

unread,
Apr 26, 2010, 5:20:57 PM4/26/10
to r-h...@r-project.org
Hi all,

One subtlety is that the drop argument only works if you specify 2 or
more indices e.g. [i, j, ..., drop=F]; but not for a single index e.g
[i, drop=F].

Why doesn't R complain about the unused "drop=F" argument in the
single index case?

Cheers,
- Stu

a = matrix(1:10, nrow=1)
b = matrix(10:1, ncol=1)

# a1 is an vector w/o dim attribute (i.e. drop=F is ignored silently)
(a1 = a[2:5, drop=F])
dim(a1)

# a2 is an vector WITH dim attribute: a row matrix (drop=F works)
(a2 = a[, 2:5, drop=F])
dim(a2)

# b1 is an vector w/o dim attribute (i.e. drop=F is ignored silently)
(b1 = b[2:5, drop=F])
dim(b1)

# b2 is an vector WITH dim attribute: a column matrix (drop=F works)
(b2 = b[2:5, , drop=F])
dim(b2)


On Mar 30, 4:08 pm, lith <minil...@gmail.com> wrote:
> > Reframe the problem. Rethink why you need to keep dimensions. I never ever had to use drop.
>
> The problem is that the type of the return value changes if you happen
> to forget to use drop = FALSE, which can easily turn into a nightmare:
>
> m <-matrix(1:20, ncol=4)
> for (i in seq(3, 1, -1)) {
>     print(class(m[1:i, ]))}
>
> [1] "matrix"
> [1] "matrix"
> [1] "integer"
>
> ______________________________________________
> R-h...@r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-h...@r-project.org mailing list
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.

--
You received this message because you are subscribed to the Google Groups "R-help-archive" group.
To post to this group, send email to r-help-...@googlegroups.com.
To unsubscribe from this group, send email to r-help-archiv...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/r-help-archive?hl=en.

Charles C. Berry

unread,
Apr 26, 2010, 9:40:26 PM4/26/10
to Stu, r-h...@r-project.org
On Mon, 26 Apr 2010, Stu wrote:

> Hi all,
>
> One subtlety is that the drop argument only works if you specify 2 or
> more indices e.g. [i, j, ..., drop=F]; but not for a single index e.g
> [i, drop=F].

Wrong.

> a <- structure(1:5,dim=5)
> dim(a)
[1] 5
> dim(a[2:3,drop=F]) # don't drop regardless
[1] 2
> dim(a[2,drop=F]) # dont' drop regardless
[1] 1
> dim(a[2:3,drop=T]) # no extent of length 1
[1] 2
> dim(a[2,drop=T]) # drop, extent of length 1
NULL


>
> Why doesn't R complain about the unused "drop=F" argument in the
> single index case?

In the example you give (one index for a two-dimension array), vector
indexing is assumed. For vector indexing, drop is irrelevant.

HTH,

Chuck
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cbe...@tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901

Matthew Keller

unread,
Apr 26, 2010, 10:05:58 PM4/26/10
to Charles C. Berry, r-h...@r-project.org, Stu
Rolf: "Well then, why don't you go away and design and build your own
statistics and data analysis language/package to replace R?"

What a nice reply! The fellow is just trying to understand R. That
response reminds me of citizens of my own country who cannot abide by
any criticism of the USA: "If you don't like it, why don't you leave?"
Classy.

I have sympathies with the author. When I first began using R
(migrating from Matlab), I also found the vector concept strange,
especially because I was doing a lot of matrix algebra back then and
didn't like the concept of conflating a row vector with a column
vector. But I've since gotten used to it and can hardly remember why I
struggled with this early on. Perhaps your experience will be similar.

Best of luck!

Matt
--
Matthew C Keller
Asst. Professor of Psychology
University of Colorado at Boulder
www.matthewckeller.com

______________________________________________
R-h...@r-project.org mailing list
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.

Stuart Andrews

unread,
Apr 26, 2010, 10:36:50 PM4/26/10
to Charles C. Berry, r-h...@r-project.org

Thanks Charles, for clarifying.

My statement holds for matrices, which are 2 dimensional. And, as you
mentioned, a single index implies vector indexing where the drop
argument doesn't make sense. I am somewhat relieved, given this new
understanding.

But I am still puzzled as to why R doesn't complain about the unused
"drop=F" argument. Since this argument is nonsensical, R should tell
me this, no? For example, when I add an unrecognized argument to the
ls() function I get the following:

> ls(nonsense="42")
Error in ls(nonsense = "42") : unused argument(s) (nonsense = "42")

Cheers,
- Stu

Charles C. Berry

unread,
Apr 27, 2010, 12:15:01 AM4/27/10
to Stuart Andrews, r-h...@r-project.org
On Mon, 26 Apr 2010, Stuart Andrews wrote:

>
> Thanks Charles, for clarifying.
>
> My statement holds for matrices, which are 2 dimensional. And, as you
> mentioned, a single index implies vector indexing where the drop argument
> doesn't make sense. I am somewhat relieved, given this new understanding.
>
> But I am still puzzled as to why R doesn't complain about the unused "drop=F"
> argument. Since this argument is nonsensical, R should tell me this, no?

I take your point that user who was intending to type a vector subscript
would not also bother to type 'drop=FALSE', and from the POV of the user
typing "a[1:3,drop=F]" at the keyboard, it makes good sense to me to have
R tip him/her off to a potential goof.

But subscripts get used a lot down deep in the code, so there are other
considerations, I suspect. If you are interested in this from a 'code
development' POV, you might repost to R-devel to ask what might have lead
to this 'feature'. I have two guesses:

1) subscripting is a pretty low-level operation and checking for this
particular case is not important enough to justify the added code and
perhaps slowing things down.

2) a construction like

do.call("[", c(list(x), args, list(drop = FALSE)))

will work without comment as long as the 'args' argument delivers a valid
subscript or set of subscripts (valid for 'x') and the drop = FALSE
will always be innocuous even if it is superfluous.


> For example, when I add an unrecognized argument to the ls() function I get
> the following:
>
>> ls(nonsense="42")
> Error in ls(nonsense = "42") : unused argument(s) (nonsense = "42")

Priorities! Failure of argument matching is a critical error. Having a
valid but superfluous argument is not, and there are loads of settings in
which no error is raised like

> y <- rnorm(3)
> x <- 1:3
> lm(y~x,data=list("blah"))

which arguably reveals a mistake by the user of the kind that having the
unneeded 'drop=FALSE' suggested.
>> > > listhttps://stat.ethz.ch/mailman/listinfo/r-help

Peter Ehlers

unread,
Apr 27, 2010, 1:02:20 AM4/27/10
to Matthew Keller, r-h...@r-project.org, Stu, Charles C. Berry
On 2010-04-26 20:05, Matthew Keller wrote:
> Rolf: "Well then, why don't you go away and design and build your own
> statistics and data analysis language/package to replace R?"
>
> What a nice reply! The fellow is just trying to understand R. That
> response reminds me of citizens of my own country who cannot abide by
> any criticism of the USA: "If you don't like it, why don't you leave?"
> Classy.

Let's not take things too far out of context. Rolf was replying
to someone who was 'very annoyed' that R has the concept of a
vector. That poster felt that, since a vector is just a special
case of a matrix, there was no need for the vector concept. That
poster had also not yet read enough to realize that there *is*
the 'drop=FALSE' argument.

It's a bit strong to complain that R was not designed to suit
your own needs and yours alone, never mind what others might
think.

I have found R-core to be quite receptive to intelligent and
well thought-out suggestions. But "It is very annoying!" is
not the right approach.

-Peter Ehlers
>>>> Â Â print(class(m[1:i, ]))}
>>>>
>>>> [1] "matrix"
>>>> [1] "matrix"
>>>> [1] "integer"
>>>>
>>>> ______________________________________________
>>>> R-h...@r-project.org mailing
>>>> listhttps://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting
>>>> guidehttp://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> ______________________________________________
>>> R-h...@r-project.org mailing list
>>> 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.
>>>
>>
>> Charles C. Berry               (858) 534-2098
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Dept of Family/Preventive
>> Medicine
>> E mailto:cbe...@tajo.ucsd.edu        UC San Diego
>> http://famprevmed.ucsd.edu/faculty/cberry/ Â La Jolla, San Diego 92093-0901
>>
>>
>> ______________________________________________
>> R-h...@r-project.org mailing list
>> 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 Ehlers
University of Calgary

Patrick Burns

unread,
Apr 27, 2010, 4:13:53 AM4/27/10
to r-h...@r-project.org, stu.a...@gmail.com
Two points:

the 'drop' argument is not unrecognized,
just superfluous.

There are one-dimensional cases where the
'drop' argument is not superfluous:

factor(letters)[1:5, drop=TRUE]
factor(letters)[1:5, drop=FALSE]

So my guess is that it would be an exceptional
amount of work to get such a warning correct,
and would possibly cause a substantial performance
hit in one of the most used functions in the
language.
>>>> listhttps://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting
>>>> guidehttp://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> ______________________________________________
>>> R-h...@r-project.org mailing list
>>> 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.
>>>
>>
>> Charles C. Berry (858) 534-2098
>> Dept of Family/Preventive Medicine
>> E mailto:cbe...@tajo.ucsd.edu UC San Diego
>> http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
>>
>
> ______________________________________________
> R-h...@r-project.org mailing list
> 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.
>

--
Patrick Burns
pbu...@pburns.seanet.com
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')
Reply all
Reply to author
Forward
0 new messages