I would like to use dplyr::mutate
to add two named columns to a dataframe simulataneously and with a single function call. Consider the following example
library(dplyr)
n <- 1e2; M <- 1e3
variance <- 1
x <- rnorm(n*M, 0, variance)
s <- rep(1:M, each = n)
dat <- data.frame(s = s, x = x)
ci_studclt <- function(x, alpha = 0.05) {
n <- length(x)
S_n <- var(x)
mean(x) + qt(c(alpha/2, 1 - alpha/2), df = n-1)*sqrt(S_n / n)
}
ci_studclt(x)
Trying something like the below returns an error, since obviously two values are produced and cannot be inserted into a single atomic-type column.
dat %>%
group_by(s) %>%
mutate(ci = ci_studclt(x, variance))
It seems one option is to insert a list column then unnest_wider
and that this is easier with data.table or the specific case of splitting a string column into two new columns.
In my example, a confidence interval (lower and upper bound) come out of a function and I would like to directly add both as new columns to dat
e.g. calling the columns ci_lower
and ci_upper
.
Is there a straightforward way of doing this with dplyr
or do I need to insert the elements as a list column then unnest?
NB Keep in mind that the confidence interval values are a function of a group of simulated values x
, grouped by s
; the CI values should be constant within a group.