To find the mean of list elements we need to unlist those elements. For example, if we have a list named as List that contains three elements of equal or different sizes such element1, element2, and element3 then we can find the mean of all the list elements by using mean(unlist(List)).
Calculating Average of List in R. To calculate the average of a List in R, use the sapply() and mean() function. The sapply() function applies a function to all the elements of the input. To create a list in R, use the list() function.
Description. lapply returns a list of the same length as X , each element of which is the result of applying FUN to the corresponding element of X .
I have a list and I want to use lapply()
to compute the mean of its elements. For example, for the seventh item of the list I have:
>list[[7]]
[1] 1 1 1 1 1 1 1 1 1 1
and my output should be:
> mean(temp[[7]][1:10])
[1] 1
But when I use lapply()
like below the result would be something else. What should I do?
> lapply(list[[7]][1:10],mean)
[[1]]
[1] 1
[[2]]
[1] 1
.
.
.
[[10]]
[1] 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With