[R] Data.frame Vs Matrix Vs Array: Definitions Please

Ivan Calandra ivan.calandra at uni-hamburg.de
Wed Oct 27 16:07:54 CEST 2010



Le 10/27/2010 15:45, Gabor Grothendieck a écrit :
> On Wed, Oct 27, 2010 at 8:38 AM, Ivan Calandra
> <ivan.calandra at uni-hamburg.de>  wrote:
>> What I don't understand is why vectors (with more than one value) don't have
>> dimensions. They look like they do have 1 dimension. For me no dimension
>> would be a scalar. Like in geometry: a point has no dimension, a line has 1,
>> a square has 2, a cube 3 and so on. Is it because of some internal process?
>> The intuitive geometry way of thinking is not programmatically relevant?
>>
> Maybe you used APL before.   The basic structure in that language is
> an array but that is not the case for R. The basic structure for data
> is a vector and more complex data objects are build from that.  An
> array is a more complex object than a vector.  A 1d array is not the
> same as a vector.  Dimensions are an additional concept unlike APL.
I've never used any other language before. It's just that I compare the 
printing of an object to a geometric object, which means that a vector 
of length > 1 has 1 dimension. In my mind, as I said, a point has no 
dimension, a line has 1, a square has 2, a cube 3 and so on. But now, I 
kind of understand that in R, dimensions do not really correspond to the 
"shape" of an object, it's just an attribute. I'll then just accept that 
1d array are not vectors and that vectors have NULL dimension (which is 
not zero I guess!)
>>>> I would also add these:
>>>> - the components of a vector have to be of the same mode (character,
>>>> numeric, integer...)
>>> however, a list with no attributes is a vector too so this is a vector:
>>>
>>>     >     vl<- list(sin, 3, "a")
>>>     >     is.vector(vl)
>>>     [1] TRUE
>>>
>>> A vector may not have attributes so arrays and factors are not vectors
>>> although they are composed from vectors.
>> That's also completely unexpected for me! What is then a vector?! And then
>> the difference between a vector and a list?! I mean, in practice, it's not
>> so important, my understanding is probably enough for what I'm doing in R,
>> but I'd like to understand how it works.
> A list is really a vector of pointers so the components are of the
> same type.  Its just that you can't access the pointer nature of the
> components.  For example, you can have a matrix based on a list.  We
> have added a dimension to the list so it becomes an array even though
> its based on a list:
>
>> m<- matrix(list(sin, "a", 1, list(1:3)), 2, 2)
>> dput(m)
> structure(list(.Primitive("sin"), "a", 1, list(1:3)), .Dim = c(2L,
> 2L))
>> m
>       [,1] [,2]
> [1,] ?    1
> [2,] "a"  List,1
>> is.array(m)
> [1] TRUE
>> class(m)
> [1] "matrix"
>
>
And even though each element has a different mode! I expected all 
elements to be converted to a single character vector with 2d.
Good that I never have such extreme cases in my data! But it's something 
to keep in mind to ensure that every object I create is what I want it to be
>> Also you wrote that a vector may not have attributes. I might be wrong (and
>> certainly am), but aren't names attributes? So with is a named list still a
>> vector:
>> my.list<- list(num=1:3, let=LETTERS[1:2])
>> names(my.list)
>> [1] "num" "let"
>> is.vector(my.list)
>> [1] TRUE
> names don't count. Neither does class.
Thanks again!
Ivan

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php



More information about the R-help mailing list