Problem calculating confidence intervals



Hello dear R Community,

I am having some issues calculating the 95% confidence intervals for the mean prices of diamonds according to their depth. The data used were taken from the “Diamond” database ( .

The Problem:
I don’t know why, but I keep getting missing values (NA’s) for my confidence intervals. I tried to calculate the confidence intervals with the R function “ci.mean”
(, but I got even more missing values.

It would be great if anyone could tell me why this keeps happening.

Here is my code:

diamonds %>%
  group_by(depth) %>%
    mean = mean(price),
    sd = sd(price),
    se = sdev / sqrt(length(price)),
    t <- qt(1-.025, df=(length(price)-1)),
    ci.lower = m - (t * stderr),
    ci.upper = m + (t * stderr)) -> results 

Many many thanks in advance!



I think I figured it out. I get the missing values in cases where there was only one observation for the depth. Confidence intervals therefore couldn’t be calculated and R returned NA’s.

Thank you anyway!