Kim Jenkins
Kim Jenkins

Reputation: 438

r function to compute the mean for values in a list

I have an r object that stores 6 values as shown below.

  acc1[[1]]$overall[1]
  0.9657
  acc1[[2]]$overall[1]
  0.96
  acc1[[3]]$overall[1]
  0.96
  acc1[[4]]$overall[1]
  0.94
  acc1[[5]]$overall[1]
  0.96
  acc1[[6]]$overall[1]
  0.95

I am curious to know if there is an efficient way to compute the mean of these values. I tried sapply(acc1$overall[1], mean) and it did not work. Any tips or suggestions are much appreciated.

Upvotes: 1

Views: 89

Answers (1)

Kim Jenkins
Kim Jenkins

Reputation: 438

Thanks to 李哲源ZheyuanLi, & Paul, both solutions worked , however I added ,na.rm = TRUE to remove any missing values.

mean( sapply(acc1, function (x) x$byClass[4]),na.rm = TRUE ) 

Upvotes: 1

Related Questions