我有一个密度分布。x
列表示x轴上的值,而y
列表示相应的密度。
我想找到第2.5个和97.5个百分位数,以及它们对应的x值。结束数据帧应该看起来像cri
,并填充了x
列。
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
创建于2022-09-13由reprex包(v2.0.1(
假设存在完全匹配,则可以使用match
:
cri <- df %>%
summarise(lwr = quantile(y, probs = 0.025),
upr = quantile(y, probs = 0.975),
mean = mean(y)) %>%
pivot_longer(cols = everything()) %>%
mutate(exact_x = df$x[match(value, df$y)]) %>%
rowwise %>%
mutate(closest_x = df$x[which.min(abs(value - df$y))]) %>%
ungroup()
cri
# # A tibble: 3 × 4
# name value exact_x closest_x
# <chr> <dbl> <dbl> <dbl>
# 1 lwr 0.00000122 -9.8 -9.8
# 2 upr 0.197 -0.300 -0.300
# 3 mean 0.0498 NA 3.3
另一种查找值的方法是使用left_join
。
library(dplyr)
library(tidyr)
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()) %>%
left_join(., df, by = c("value"="y"))
# A tibble: 4 x 3
name value x
<chr> <dbl> <dbl>
1 lwr 0.00000122 -9.8
2 lwr 0.00000122 9.8
3 upr 0.197 -0.300
4 mean 0.0498 NA