[R] r data structures

Schumacher, Jay S jays at neo.tamu.edu
Thu Aug 16 22:53:16 CEST 2012


yes, thank you,     conceptual model (rather than formal dimension attribute) is where i'm coming from at this point.

----------------------------------------

It would be helpful to distinguish between a formal dimension attribute,
and a (personal) conceptual model of whether or not any particular R
object, or type of object, has dimension. Mention of data frames having
dimension can be found in the help page for the dim() function.


> foo <- 1:10
> is.vector(foo)
[1] TRUE
> dim(foo)
NULL
> attributes(foo)
NULL
> str(foo)
 int [1:10] 1 2 3 4 5 6 7 8 9 10
> length(foo)
[1] 10




> bah <- matrix(1:10, nrow=2)
> is.vector(bah)[1] FALSE
> dim(bah)
[1] 2 5
> attributes(bah)
$dim
[1] 2 5
> str(bah)
 int [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
> length(bah)
[1] 10



The vector does not have a formal dimension ("dim") attribute, but the
matrix does.



Regarding data frames and lists:

##
## data frame
##
> junk <- data.frame(a=1:3,b=1:3)
> str(junk)
'data.frame':   3 obs. of  2 variables:
 $ a: int  1 2 3
 $ b: int  1 2 3
> attributes(junk)
$names
[1] "a" "b"

$row.names
[1] 1 2 3

$class
[1] "data.frame"

> dim(junk)
[1] 3 2

##
## list
##
> glug <- list(a=1, b=letters[3])
> 
> str(glug)
List of 2
 $ a: num 1
 $ b: chr "c"
> attributes(glug)
$names
[1] "a" "b"

> dim(glug)
NULL
> length(glug)
[1] 2

Conceptually, I would consider data frames to have two dimensions (rows
and columns). They do not have a formal "dim" attribute, but the dim()
function does return a value.

I personally do not think of lists as having dimension -- I never ask
myself, what is the dimension of a list? But I do often enquire as to the
length of a list, so might, if forced to, admit that lists have one
dimension, length. But I do not think it is helpful to think of lists as
having dimension. Certainly, lists do not have two dimensions.




-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 8/16/12 11:49 AM, "Schumacher, Jay S" <jays at neo.tamu.edu> wrote:

>
>
>hi,
>  i'm trying to understand r data structures.  i see that vectors,
>matrix, factors and arrays have a "dimension."
>  there seems to be no mention of dimensionality anywhere for lists or
>dataframes.  can i consider lists and frames to be of fixed dimension 2?
>thanks,
> jay s
>
>______________________________________________
>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.



More information about the R-help mailing list