confidence interval in r

To calculate a confidence interval in R, you can use the t.test() function. This function requires a sample of data with a numeric variable, and can calculate a confidence interval based on a specified level of confidence:

main.r
# create a sample dataset
set.seed(123)
data <- rnorm(100)

# calculate a 95% confidence interval
t.test(data)$conf.int
120 chars
7 lines

This will give you the lower and upper bounds of the confidence interval. If you want to specify a different level of confidence, you can pass it as an argument:

main.r
# calculate a 90% confidence interval
t.test(data, conf.level = 0.90)$conf.int
79 chars
3 lines

You can also calculate a confidence interval for the difference between two groups using the same t.test() function:

main.r
# create two sample datasets
set.seed(123)
group1 <- rnorm(50, mean = 10, sd = 2)
group2 <- rnorm(50, mean = 12, sd = 2)

# calculate a 95% confidence interval for the difference in means
t.test(group1, group2)$conf.int
220 chars
8 lines

This will give you the lower and upper bounds of the confidence interval for the difference between the two group means.

gistlibby LogSnag