If we need to calculate the mean
, se
for each row, then use apply
with MARGIN = 1
. Loop over the list
elements with lapply
and then do the loop over the rows with apply
calculate_function <- function(x) {
cbind(x['Year'], t(apply(x[-1], 1, function(u)
c(mean = mean(u, na.rm = TRUE),
se = plotrix::std.error(u, na.rm = TRUE)))))
}
lapply(my_list, calculate_function)
-output
#[[1]]
# Year mean se
#1 2015 0.6000000 0.4000000
#2 2016 0.5633333 0.2152776
#3 2017 0.7666667 0.2185813
#4 2018 1.0000000 0.5000000
#[[2]]
# Year mean se
#1 2015 0.6000000 0.4000000
#2 2016 0.5633333 0.2152776
#3 2017 0.7666667 0.2185813
#4 2018 1.0000000 0.5000000
data
my_list <- list(structure(list(Year = 2015:2018, tex21.1 = c(0.2, 0.3, 0.5,
NA), tex21.2 = c(NA, 0.4, 1.2, 1.5), tex21.3 = c(1, 0.99, 0.6,
0.5)), class = "data.frame", row.names = c(NA, -4L)), structure(list(
Year = 2015:2018, tex21.1 = c(0.2, 0.3, 0.5, NA), tex21.2 = c(NA,
0.4, 1.2, 1.5), tex21.3 = c(1, 0.99, 0.6, 0.5)),
class = "data.frame", row.names = c(NA,
-4L)))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…