Reputation:
I have a list where each element is an array. Example data:
set.seed(24)
data <- list(individual1 = array(rnorm(3 * 3 * 2, 60),
dim = c(3, 3, 2), dimnames = list(NULL, NULL, c("rep1", "rep2"))),
individual2 = array(rnorm(3 * 3 * 2, 60), dim = c(3, 3, 2),
dimnames = list(NULL, NULL, c("rep1", "rep2")) ) )
I would like to find the length of the entire list. However, when I use length, I get 2, whereas I want 4 because there are 4 arrays. Is there another way to determine length for my question?
>length(data)
2
Upvotes: 2
Views: 1369
Reputation: 50668
Like this?
sum(sapply(data, function(x) dim(x)[3]))
#[1] 4
Explanation: Your list
in fact only contains 2 elements. The dimension of every list
element is
lapply(data, dim)
#$individual1
#[1] 3 3 2
#
#$individual2
#[1] 3 3 2
In other words, every list
elements has 2 3x3
arrays. We can therefore get the total number of 3x3
arrays in the list
by summing the number of 3x3
arrays from every list
element.
Upvotes: 1