I couldn’t think how to title this one well, what I have is a vector of objects generated by a function (specifically midPci) of this form:
$conf.int
[1] 0.4726 0.6466
attr(,"conf.level")
[1] 0.95
$conf.int
[1] 0.1181 0.2566
attr(,"conf.level")
[1] 0.95
What I want to do is strip out the two values of $conf.int into vectors giving the upper and lower confidence intervals. Obviously I can do this with a loop, but I figured there was probably a better way to do this?
CIs <- structure(list(conf.int = structure(c(0.4696, 0.6501), conf.level = 0.95),
conf.int = structure(c(0.5266, 0.7081), conf.level = 0.95), conf.int = structure(c(0.4441,
0.6196), conf.level = 0.95), conf.int = structure(c(0.4181, 0.5891), conf.level = 0.95),
conf.int = structure(c(0.4726, 0.6466), conf.level = 0.95), conf.int = structure(c(0.1181,
0.2566), conf.level = 0.95), conf.int = structure(c(0, 0.000748652688017826), conf.level =
0.95)), .Names = c("conf.int", "conf.int", "conf.int", "conf.int", "conf.int", "conf.int",
"conf.int" ))
In case it’s not clear what I mean, I’ve done this:
# Calculate CIs
CIs <- mapply(midPci, k, n, conf.level=0.95)
And I can get the individual values I’m after out by doing CIs[1]$conf.int[1], etc., but I want to get the whole lot out in one go. Can I do this?
This produces a matrix:
This also produces a matrix, although it is the ‘wide version’. The
cfunction applied to a vector just returns itself , and you could have usedas.vectororIand gotten the same result. It is really the functionsimplify2array(CIs)that is called bysapplythat is doing the work:And had we used
do.call(cbind, CIs)we would have gotten the ‘wide version’ as well.