tdsmith / aRrgh

A newcomer's (angry) guide to data types in R
Other
307 stars 14 forks source link

length(foo) #2

Closed vincentarelbundock closed 11 years ago

vincentarelbundock commented 11 years ago
Do not use length(foo), which will for reasons unexplained tell you how many columns you have;

Because dataframes, as you note above, are stored as lists of column vectors. The length of that list is the number of columns.

tdsmith commented 11 years ago

Ah! I'm still trying to understand lists and hadn't made this connection yet, so this was clarifying. Thanks!

vincentarelbundock commented 11 years ago

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 :)

tdsmith commented 11 years ago

Thanks; I'm glad this has value!

Explained in 3fffcece.

vincentarelbundock commented 11 years ago

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
cubranic commented 10 years ago

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.