[英]Is it possible to `for` loop the `sapply` in R?
I was wondering why my object CI
doesn't correctly return the full (11 paired answers) outputs from the for()
loop in the following function? 我想知道为什么我的对象
CI
无法在以下函数中正确返回for()
循环的完整输出(11对答案)? Instead, the CI
returns 11 single numbers. 相反,
CI
返回11个单号。
N = 30 ; df = 118 ; d = 1
f <- function (ncp, alpha, q, df) {
abs(suppressWarnings(pt(q = d*sqrt(N), df = df, ncp, lower.tail = FALSE)) -
alpha)
}
a = mapply(c, as.list(20:30), as.list(-20:-30), SIMPLIFY = FALSE) # a list of paired values
CI <- numeric(length(a))
for(i in 1:length(a)){
CI[i] = sapply(c(0.025, 0.975),
function(x) optimize(f, interval = a[[i]], alpha = x, q = d*sqrt(N), df = df, tol = 1e-10)[[1]])
}
CI # just returns one paired of the 11 paired answers expected!
How about: 怎么样:
N = 30 ; df = 118 ; d = 1
f <- function (ncp, alpha, q, df) {
abs(suppressWarnings(pt(q = d*sqrt(N), df = df, ncp, lower.tail = FALSE)) -
alpha)
}
a = mapply(c, as.list(20:30), as.list(-20:-30), SIMPLIFY = FALSE) # a list of paired values
CI <- matrix(NA, 11,2)
for(i in 1:length(a)){
CI[i,] = sapply(c(0.025, 0.975),
function(x) optimize(f, interval = a[[i]], alpha = x, q = d*sqrt(N), df = df, tol = 1e-10)[[1]])
}
CI
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.