计算列表中各个元素的均值 [英] Calculate means across elements in a list
本文介绍了计算列表中各个元素的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的list
:
(mylist <- list(a = data.frame(x = c(1, 2), y = c(3, 4)),
b = data.frame(x = c(2, 3), y = c(4, NA)),
c = data.frame(x = c(3, 4), y = c(NA, NA))))
$a
x y
1 1 3
2 2 4
$b
x y
1 2 4
2 3 NA
$c
x y
1 3 NA
2 4 NA
由purrr::map()
创建的
.如何计算相应单元格中的值均值?即
which is created by purrr::map()
. How can I calculate the means of values in the corresponding cells? i.e.
x y
1 2 3.5
2 3 4
其中
mean(c(1, 2, 3), na.rm = T) # = 2
mean(c(2, 3, 4), na.rm = T) # = 3
mean(c(3, 4, NA), na.rm = T) # = 3.5
mean(c(4, NA, NA), na.rm = T) # = 4
感谢您的帮助!
推荐答案
一个purrr
选项
library(purrr)
map_df(transpose(mylist), ~rowMeans(as.data.frame(.x), na.rm = TRUE))
# A tibble: 2 x 2
# x y
# <dbl> <dbl>
#1 2 3.5
#2 3 4
这篇关于计算列表中各个元素的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文