I stacked the data.table, because it's much more efficient to work with a data.table in long format. I also prefer to set the confidence limits to the same value as the mean, if all values are equal. Adjust as you like.
library(boot)
library(data.table)
DT <- data.table(column1=1:100,column2=rep(100,100))
DT <- data.table(stack(DT))
resample.number=1000
confidence=0.95
sample.mean <- function(indata,x){mean(indata[x])}
ci.mean <- function(x, resample.number,confidence) {
if(length(unique(x)) > 1) {
temp <- boot.ci(boot(x,statistic = sample.mean,R = resample.number), conf = confidence, type = "perc")$percent
list(mean=mean(x),lwr=temp[,4],upr=temp[,5])
} else {
list(mean=mean(x),lwr=mean(x),upr=mean(x)
}
}
set.seed(42)
DT[,ci.mean(values,resample.number,confidence),by=ind]
# ind mean lwr upr
#1: column1 50.5 44.92305 55.93949
#2: column2 100.0 100.00000 100.00000
Note that boot.ci
just gives a warning and returns NA
values, if all values are equal. There is no error and if you can work with NAs, there is no need for the if
condition.