Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply mean function over elements of a list in R

Tags:

People also ask

How do you apply mean to a list in R?

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

Can you get the mean of a list in R?

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.

Which command applies the function to each element in the given vector and returns a list that contains all the results?

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