看到更大的列表上有广泛的时间范围真是很有趣:
library(plyr)
library(data.table)
library(dplyr)
library(tidyr)
library(tibble)
set.seed(976933858)
l <- unname(split(runif(1e5), sample(1e4, 1e5, TRUE)))
f0 <- function(l) diff(c(0, cumsum(unlist(l)[order(sequence(lengths(l)))])[cumsum(lens <- rev(cumsum(rev(tabulate(lengths(l))))))]))/lens
f1 <- function(l) sapply(transpose(l), mean, na.rm = TRUE)
f2 <- function(l) as.numeric(tapply(unlist(l), sequence(lengths(l)), mean))
f3 <- function(l) rowMeans(sapply(l, `length<-`, max(lengths(l))), na.rm = TRUE)
f4 <- function(L) rowMeans(sapply(L, `[`, 1:max(lengths(L))), na.rm = TRUE)
f5 <- function(mylist) {
max_L <- max(lengths(mylist))
rowMeans(sapply(mylist, function(x) c(x, rep(NA, max_L - length(x)))), na.rm = T)
}
f6 <- function(l) l %>% enframe %>% unnest(value) %>% group_by(grp = rowid(name)) %>% summarise(value = mean(value)) %>% pull(value)
f7 <- function(l) unname(colMeans(rbind.fill.matrix(sapply(l, t)), na.rm = TRUE))
f8 <- function(L) rowMeans(do.call("cbind", lapply(L, ts)), na.rm = TRUE)
microbenchmark::microbenchmark(
jblood94 = f0(l),
Mael = f1(l),
onyambu1 = f2(l),
onyambu2 = f3(l),
G.Grothendieck1 = f4(l),
benson23 = f5(l),
akrun = f6(l),
onyambu3 = f7(l),
G.Grothendieck2 = f8(l),
check = "identical"
)
rowMeans()
和na.rm = TRUE
。 - Basclass(a)
是数字,即class(c(1))
是数字。现在你明白为什么给出的解决方案不起作用了吗? - Onyambudput(x)
将您的数据包含在内。您的数据无法重现,即运行dput(list(a, b, c, d, e)))
,复制输出并粘贴到您的问题中。简单地说,接受任何给定解决方案来关闭此问题,同时保持原始问题,并打开一个新问题以处理数据。 - Onyambu