Skip to content
Prev 303545 / 398503 Next

r data structures

I don't disagree with Michael, but I would add that to me it also depends.

If one thinks in terms of subsetting an object (for objects that can be
subsetted)

To subset a vector, one supplies *one* value for the index:
  myvector[3]
  myvector[ 2:5 ]
are valid statements.

Similarly for a list
  mylist[4]
  mylist[ c(1,3) ]
are valid statements.

Whereas for a matrix or data frame, one must supply *two* index values
(even if one of them may be omitted)
  mydf[ 1 , 3 ]
  mydf[   , 5 ]

  mymat[ 2:5 ,     ]
  mymat[ 3   , 4:6 ]
are valid statements.

So from that perspective, it's reasonable to think of vectors and lists
having one dimension, and matrices and data frames as having two.

Formally, it's a little different, since the underlying structure of a
data frame is actually a list:
  > junk <- data.frame(a=1:3, b=letters[1:3] )
  > is.list(junk)  [1] TRUE
and lists do not, formally, have a dimension attribute.

I would add that in everyday more or less casual work with R, matrices,
arrays, and data frames are the only objects where I find it useful to
think about their dimension. Otherwise it's length. Or maybe neither, for
some objects.

-Don