Reputation: 63259
From the following code we can see there is a 2D array of list
's:
> coldists[3,4]
[[1]]
mean sd
0.5752512 0.4067640
> class(coldists[3,4][1])
[1] "list"
> class(coldists[3,4][[1]])
[1] "numeric"
My confusion is: why is the length of the list reported as [1]
instead of [2]
?
length(coldists[3,4])
[1] 1
After all there appear to be 2 entries (mean
and sd
) in that list ..
Upvotes: 1
Views: 55
Reputation: 9107
It is a list containing a vector with two elements.
x <- list(c(mean = 0.5752512, sd = 0.4067640))
x
#> [[1]]
#> mean sd
#> 0.5752512 0.4067640
Because it only contains one vector, it has length one.
length(x)
#> [1] 1
However, when we get the length of the vector, it has length 2.
length(x[[1]])
#> [1] 2
When we use single brackets, it gets a sublist rather than extracting the vector. This sublist is still a length one list containing a vector with two elements.
length(x[1])
#> [1] 1
Upvotes: 3
Reputation: 6671
Here, coldists[3,4]
is a list with one element. Inside this unique list element there is a vector of length 2 (mean, sd).
If you want to have the length of the vector inside the list, you need to specify it like :
length(coldists[3,4][[1]])
Upvotes: 2