[R] Confusing concept of vector and matrix in R

Charles C. Berry cberry at tajo.ucsd.edu
Tue Apr 27 06:15:01 CEST 2010


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.

HTH,

Chuck

>
> Cheers,
> - Stu
>
>
> On Apr 26, 2010, at 9:40 PM, Charles C. Berry wrote:
>
>> 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
>> > 
>> > 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... at 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... at 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-help at 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:cberry at tajo.ucsd.edu	            UC San Diego
>> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
>> 
>

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



More information about the R-help mailing list