R 中的元素均值

发布于 2024-09-14 22:45:46 字数 315 浏览 5 评论 0 原文

在 R 中,我有两个向量:

a <- c(1, 2, 3, 4)
b <- c(NA, 6, 7, 8)

如何在没有循环的情况下找到两个向量的元素均值,删除 NA?即我想获取我知道函数 mean() 的向量

(1, 4, 5, 6)

,我知道参数 na.rm = 1。但我不知道如何将这些东西组合在一起。可以肯定的是,实际上我有数千个向量,其中 NA 出现在不同的地方,因此任何与维度相关的解决方案都不起作用。谢谢。

In R, I have two vectors:

a <- c(1, 2, 3, 4)
b <- c(NA, 6, 7, 8)

How do I find the element-wise mean of the two vectors, removing NA, without a loop? i.e. I want to get the vector of

(1, 4, 5, 6)

I know the function mean(), I know the argument na.rm = 1. But I don't know how to put things together. To be sure, in reality I have thousands of vectors with NA appearing at various places, so any dimension-dependent solution wouldn't work. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为一人 2024-09-21 22:45:47

怎么样:

rowMeans(cbind(a, b), na.rm=TRUE)

或者

colMeans(rbind(a, b), na.rm=TRUE)

how about:

rowMeans(cbind(a, b), na.rm=TRUE)

or

colMeans(rbind(a, b), na.rm=TRUE)
梦中的蝴蝶 2024-09-21 22:45:47

我不太确定你在要求什么,但它确实

apply(rbind(a,b),2,mean,na.rm = TRUE)

符合你的要求吗?

I'm not exactly sure what you are asking for, but does

apply(rbind(a,b),2,mean,na.rm = TRUE)

do what you want?

倚栏听风 2024-09-21 22:45:47

使用 purrrtidyverse 解决方案:

library(purrr)
a <- c(1, 2, 3, 4)
b <- c(NA, 6, 7, 8)

# expected:
c(1, 4, 5, 6) 
#> [1] 1 4 5 6

# actual:
map2_dbl(a,b, ~mean(c(.x,.y), na.rm=T)) # actual
#> [1] 1 4 5 6

对于任意数量的向量:

>  pmap_dbl(list(a,b, a, b), compose(partial(mean, na.rm = T), c))
 [1] 1 4 5 6

A tidyverse solution usign purrr:

library(purrr)
a <- c(1, 2, 3, 4)
b <- c(NA, 6, 7, 8)

# expected:
c(1, 4, 5, 6) 
#> [1] 1 4 5 6

# actual:
map2_dbl(a,b, ~mean(c(.x,.y), na.rm=T)) # actual
#> [1] 1 4 5 6

And for any number of vectors:

>  pmap_dbl(list(a,b, a, b), compose(partial(mean, na.rm = T), c))
 [1] 1 4 5 6
秋凉 2024-09-21 22:45:47

另一个选项是 collapse::fmean,它默认为矩阵上的按列均值和 na.rm = TRUE

fmean(rbind(a, b))
#[1] 1 4 5 6

Benchmark

向量 ab (长度 = 4):

microbenchmark::microbenchmark(
  collapse = fmean(rbind(a, b)),
  rowMeans = rowMeans(cbind(a, b), na.rm=TRUE),
  colMeans = colMeans(rbind(a, b), na.rm=TRUE),
  purrr = purrr::map2_dbl(a,b, ~mean(c(.x,.y), na.rm=T)),
  apply = apply(rbind(a,b),2,mean,na.rm = TRUE)
)

# Unit: microseconds
#      expr    min       lq      mean   median       uq      max neval
#  collapse  6.501   7.9020  10.72705   9.7010  10.8010   56.101   100
#  rowMeans  4.601   6.0505   9.21504   7.8010   9.4515   28.102   100
#  colMeans  4.700   5.7010   7.76410   6.8515   8.2015   27.301   100
#     purrr 94.101 104.4505 140.36694 108.8010 121.9510 2120.901   100
#     apply 50.301  55.1010  65.37305  59.9005  65.6510  156.700   100

大向量(大小 1e6):

a = sample(1e6)
b = sample(1e6)

# Unit: milliseconds
#      expr       min        lq     mean    median       uq     max neval
#  collapse  8.384401  9.621752 13.02568 10.160101 18.83060 34.2746   100
#  rowMeans 18.504201 21.513251 27.88083 23.509051 31.28925 94.2124   100
#  colMeans  8.117601  9.344551 12.69392  9.897702 12.50430 54.1703   100

Another option is collapse::fmean, which defaults to column-wise means on matrices and na.rm = TRUE:

fmean(rbind(a, b))
#[1] 1 4 5 6

Benchmark

Vectors a and b (length = 4):

microbenchmark::microbenchmark(
  collapse = fmean(rbind(a, b)),
  rowMeans = rowMeans(cbind(a, b), na.rm=TRUE),
  colMeans = colMeans(rbind(a, b), na.rm=TRUE),
  purrr = purrr::map2_dbl(a,b, ~mean(c(.x,.y), na.rm=T)),
  apply = apply(rbind(a,b),2,mean,na.rm = TRUE)
)

# Unit: microseconds
#      expr    min       lq      mean   median       uq      max neval
#  collapse  6.501   7.9020  10.72705   9.7010  10.8010   56.101   100
#  rowMeans  4.601   6.0505   9.21504   7.8010   9.4515   28.102   100
#  colMeans  4.700   5.7010   7.76410   6.8515   8.2015   27.301   100
#     purrr 94.101 104.4505 140.36694 108.8010 121.9510 2120.901   100
#     apply 50.301  55.1010  65.37305  59.9005  65.6510  156.700   100

Large vectors (size 1e6):

a = sample(1e6)
b = sample(1e6)

# Unit: milliseconds
#      expr       min        lq     mean    median       uq     max neval
#  collapse  8.384401  9.621752 13.02568 10.160101 18.83060 34.2746   100
#  rowMeans 18.504201 21.513251 27.88083 23.509051 31.28925 94.2124   100
#  colMeans  8.117601  9.344551 12.69392  9.897702 12.50430 54.1703   100
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文