Closed vincentarelbundock closed 11 years ago
Ah! I'm still trying to understand lists and hadn't made this connection yet, so this was clarifying. Thanks!
No, thank YOU for putting this together. It'll be one of the first links I send people who ask me where to learn about R :)
Thanks; I'm glad this has value!
Explained in 3fffcece.
Maybe this is not worth it, but the apparent inconsistency here is pretty interesting, I think:
> mat = matrix(1:4, nrow=2)
> dat = data.frame(mat)
> mat
[,1] [,2]
[1,] 1 3
[2,] 2 4
> dat
X1 X2
1 1 3
2 2 4
> length(mat)
[1] 4
> length(dat)
[1] 2
Because 'matrix' is a vector and 'data.frame' is a list of columns, so length
returns the (total) number of elements in the matrix (i.e., nrows x ncols) and the number of columns in the data frame.
Thankfully, nrow
and ncol
work correctly for both types.
Because dataframes, as you note above, are stored as lists of column vectors. The length of that list is the number of columns.