I have a density distribution. The x column represents the values on the x axis, while the y column represents the corresponding density.
I would like to find the 2.5th and 97.5 percentiles and their corresponding x values.
The end dataframe should look like cri with the x column filled in.
library(tidyverse)
x = seq(-10,10, 0.1)
y = dnorm(x, mean = 0, sd = 2)
df = tibble(x,y)
cri <- df %>%
summarise(lwr = quantile(y, probs = 0.025),
upr = quantile(y, probs = 0.975),
mean = mean(y)) %>%
pivot_longer(cols = everything()) %>%
mutate(x = NA)
cri
#> # A tibble: 3 x 3
#> name value x
#> <chr> <dbl> <lgl>
#> 1 lwr 0.00000122 NA
#> 2 upr 0.197 NA
#> 3 mean 0.0498 NA
Created on 2022-09-13 by the reprex package (v2.0.1)