Tip for R newbies like me: beware, the following is a list of one object :
> mylist <- list (1:10) > length (mylist) [1] 1
In this case, you are not looking for the length of the list, but its first element:
> length (mylist[[1]]) [1] 10
This is the "true" list:
> mylist <- list(1:10, rnorm(25), letters[1:3]) > length (mylist) [1] 3
In addition, it seems that R treats data.frame as a list:
> df <- data.frame (matrix(0, ncol = 30, nrow = 2)) > typeof (df) [1] "list"
In this case, you may be interested in ncol() and nrow() , rather than length() :
> ncol (df) [1] 30 > nrow (df) [1] 2
Although length() will also work (but this is a trick when your data.frame has only one column):
> length (df) [1] 30 > length (df[[1]]) [1] 2
Skippy le Grand Gourou Jan 29 '14 at 23:26 2014-01-29 23:26
source share