如何:计算 R 中每个值的百分位值
How to: calculate percentile values for each values in R
我需要计算数据集中四列中所有值的百分位排名。结果应该是这样的:
Name Value1 Percentile1 Value2 Percentile2 Value3 Percentile3 Value4 Percentile4
a X 0.000000 .... .... ....
b X 0.159272 .... .... ....
c X 1.000000 .... .... ....
d X 0.240728 .... .... ....
...
每个百分位数的格式为 6 位小数。有人可以帮忙吗?
我试过 ntile() 但它不能给我 6 个十进制数。
让我们先生成一些数据
library(tidyverse)
set.seed(1)
df <- tibble(
name = letters,
value1 = rnorm(length(letters)),
value2 = -rnorm(length(letters)),
value3 = abs(rnorm(length(letters))) )
计算百分位排名的函数(来源:https://stats.stackexchange.com/a/11928)
perc.rank <- function(x) trunc(rank(x))/length(x)
df %>% mutate(
percentile1 = perc.rank(value1),
percentile2 = perc.rank(value2),
percentile3 = perc.rank(value3)
) -> df
> df
name value1 value2 value3 percentile1 percentile2 percentile3
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 a -0.626 0.156 0.341 0.192 0.615 0.308
2 b 0.184 1.47 1.13 0.462 1 0.731
3 c -0.836 0.478 1.43 0.115 0.808 0.808
4 d 1.60 -0.418 1.98 1 0.308 0.923
我需要计算数据集中四列中所有值的百分位排名。结果应该是这样的:
Name Value1 Percentile1 Value2 Percentile2 Value3 Percentile3 Value4 Percentile4
a X 0.000000 .... .... ....
b X 0.159272 .... .... ....
c X 1.000000 .... .... ....
d X 0.240728 .... .... ....
...
每个百分位数的格式为 6 位小数。有人可以帮忙吗? 我试过 ntile() 但它不能给我 6 个十进制数。
让我们先生成一些数据
library(tidyverse)
set.seed(1)
df <- tibble(
name = letters,
value1 = rnorm(length(letters)),
value2 = -rnorm(length(letters)),
value3 = abs(rnorm(length(letters))) )
计算百分位排名的函数(来源:https://stats.stackexchange.com/a/11928)
perc.rank <- function(x) trunc(rank(x))/length(x)
df %>% mutate(
percentile1 = perc.rank(value1),
percentile2 = perc.rank(value2),
percentile3 = perc.rank(value3)
) -> df
> df
name value1 value2 value3 percentile1 percentile2 percentile3
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 a -0.626 0.156 0.341 0.192 0.615 0.308
2 b 0.184 1.47 1.13 0.462 1 0.731
3 c -0.836 0.478 1.43 0.115 0.808 0.808
4 d 1.60 -0.418 1.98 1 0.308 0.923