2

I want to get the mean over all elements of this list. I want the result to be a single number. I have tried sapply but does not provide desired result.

[[1]]
[1] 6.3

[[2]]
[1] 5.8

[[3]]
[1] 7.1

[[4]]
[1] 6.3

[[5]]
[1] 6.5

[[6]]
[1] 7.6

[[7]]
[1] 4.9

[[8]]
[1] 7.3

[[9]]
[1] 6.7

[[10]]
[1] 7.2

[[11]]
[1] 6.5

[[12]]
[1] 6.4

[[13]]
[1] 6.8

[[14]]
[1] 5.7

[[15]]
[1] 5.8

[[16]]
[1] 6.4

[[17]]
[1] 6.5

[[18]]
[1] 7.7

[[19]]
[1] 7.7

[[20]]
[1] 6

[[21]]
[1] 6.9

[[22]]
[1] 5.6

[[23]]
[1] 7.7

[[24]]
[1] 6.3

[[25]]
[1] 6.7

[[26]]
[1] 7.2

[[27]]
[1] 6.2

[[28]]
[1] 6.1

[[29]]
[1] 6.4

[[30]]
[1] 7.2

[[31]]
[1] 7.4

[[32]]
[1] 7.9

[[33]]
[1] 6.4

[[34]]
[1] 6.3

[[35]]
[1] 6.1

[[36]]
[1] 7.7

[[37]]
[1] 6.3

[[38]]
[1] 6.4

[[39]]
[1] 6

[[40]]
[1] 6.9

[[41]]
[1] 6.7

[[42]]
[1] 6.9

[[43]]
[1] 5.8

[[44]]
[1] 6.8

[[45]]
[1] 6.7

[[46]]
[1] 6.7

[[47]]
[1] 6.3

[[48]]
[1] 6.5

[[49]]
[1] 6.2

[[50]]
[1] 5.9

1 Answers1

1

The list you have seems to be fifty numerical vectors, each of length 1. If you just want the mean of all elements in the list use the unlist() function

mean(unlist(...))

Put the name of the list object where ... is. The unlist() function will break it into a single vector, in this case with a length of fifty. The mean() function simply takes the mean of a vector, that's why you need to convert your list to a vector.

# example data
listA <- list(4,6,9,6,4,5,2,4,9,8,6,3,5,2,7)
# mean of the list
mean(unlist(listA))
rg255
  • 4,119
  • 3
  • 22
  • 40