我已经收集了一个包含nls系数的数据帧的列表。这是自定义引导(实际上,打包)方法的一部分。我想计算数据帧中每个参数的平均值。
在取样xdata和ydata之后,列表将被填充到一个循环中,该循环包含:
nls(ydata ~ A*cos(2*pi*((xdata-x_0)/z))+M,start=list(A=4,M=-7,x_0=-10,z=30))
fitdata = summary(fit)$coefficients
fitresults[[i]] = fitdata该列表包含100个数据帧,如下所示:
Estimate Std. Error t value Pr(>|t|)
A 3.945959 0.1729441 22.816381 3.440064e-14
M -8.349697 0.1656195 -50.414926 5.920106e-20
x_0 -3.677582 0.5717355 -6.432313 6.194560e-06
z 33.680613 1.1314373 29.767989 4.158598e-16我想计算列表第一列中每个元素的平均值。所以,估计的A,M,x_0和z。
我玩过一些dply函数,但我搞不懂。
非常感谢!
发布于 2017-03-09 19:29:42
为了重现我的示例,我只创建了一个名为mylist的列表,其中包含两个数据格式。获得第一列的可能性很大,其中一种可能是对列表进行sapply,如下所示:
set.seed(1)
mylist <- list(list1 = data.frame(matrix(rnorm(16),
ncol = 4,
nrow = 4,
dimnames = list(row = c("A", "M", "x_0", "z"),
column = c("Estimate",
"Std.Error",
"t_Value",
"Pr(<|t|)")))),
list2 = data.frame(matrix(rnorm(16),
ncol = 4,
nrow = 4,
dimnames = list(row = c("A", "M", "x_0", "z"),
column = c("Estimate",
"Std.Error",
"t_Value",
"Pr(<|t|)")))))
mylist
$list1
Estimate Std.Error t_Value Pr...t..
A -0.6264538 0.3295078 0.5757814 -0.62124058
M 0.1836433 -0.8204684 -0.3053884 -2.21469989
x_0 -0.8356286 0.4874291 1.5117812 1.12493092
z 1.5952808 0.7383247 0.3898432 -0.04493361
$list2
Estimate Std.Error t_Value Pr...t..
A -0.01619026 0.91897737 0.61982575 -0.4781501
M 0.94383621 0.78213630 -0.05612874 0.4179416
x_0 0.82122120 0.07456498 -0.15579551 1.3586796
z 0.59390132 -1.98935170 -1.47075238 -0.1027877然后:
result <- data.frame(Estimates = apply(sapply(mylist, function(x) x[, "Estimate"]), 1, mean))
rownames(result) <- c("A", "M", "x_0", "z")
result
Estimates
A -0.321322037
M 0.563739767
x_0 -0.007203709
z 1.094591062https://stackoverflow.com/questions/42703579
复制相似问题