[R] Confusing concept of vector and matrix in R
Peter Ehlers
ehlers at ucalgary.ca
Tue Apr 27 07:02:20 CEST 2010
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
>
> 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
>
>
>
> On Mon, Apr 26, 2010 at 7:40 PM, Charles C. Berry<cberry at tajo.ucsd.edu> 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
>>
>>
>> ______________________________________________
>> 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.
>>
>>
>
>
>
--
Peter Ehlers
University of Calgary
More information about the R-help
mailing list