如何将字符串转换为聚集的分位数?

问题描述 投票:1回答:2

我有一个按组嵌套的数据框。我想将变量“ x”从其原始值转换为分位数位置(20%,40%,60%,80%,100%或1、2、3、4、5)。

这里是我正在使用的数据的示例:

df <- data.frame(x=c(1, 5, 21, 24, 43, 47, 56, 59, 68, 69, 11, 15, 25, 27, 48, 49, 51, 55, 61, 67),
                 y=c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"))

这是我尝试过的:

df$z <- aggregate(df$x, by = list(df$y), FUN = function(x) quantile(x, probs = c(0.2, 0.4, 0.6, 0.8, 1), na.rm = T))

本质上,我想创建一个看起来像这样的新变量:

df$z <- c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5)
r aggregate quantile percentile
2个回答
3
投票

在分组的数据框架上,您可以使用dplyr::ntile()

library(dplyr)

df %>%
  group_by(y) %>%
  mutate(z = ntile(x, 5))

# A tibble: 20 x 3
# Groups:   y [2]
       x y         z
   <dbl> <fct> <int>
 1     1 A         1
 2     5 A         1
 3    21 A         2
 4    24 A         2
 5    43 A         3
 6    47 A         3
 7    56 A         4
 8    59 A         4
 9    68 A         5
10    69 A         5
11    11 B         1
12    15 B         1
13    25 B         2
14    27 B         2
15    48 B         3
16    49 B         3
17    51 B         4
18    55 B         4
19    61 B         5
20    67 B         5

1
投票

我们可以将cutbreaks用作quantile

library(dplyr)  
df %>%
   group_by(y) %>%
   mutate(z = as.integer(cut(x, breaks = c(-Inf, 
       quantile(x, probs = c(0.2, 0.4, 0.6, 0.8, 1), na.rm = TRUE)))))
# A tibble: 20 x 3
# Groups:   y [2]
#       x y         z
#   <dbl> <fct> <int>
# 1     1 A         1
# 2     5 A         1
# 3    21 A         2
# 4    24 A         2
# 5    43 A         3
# 6    47 A         3
# 7    56 A         4
# 8    59 A         4
# 9    68 A         5
#10    69 A         5
#11    11 B         1
#12    15 B         1
#13    25 B         2
#14    27 B         2
#15    48 B         3
#16    49 B         3
#17    51 B         4
#18    55 B         4
#19    61 B         5
#20    67 B         5

或与base R一起使用ave

with(df, ave(x, y, FUN = function(u) as.integer(cut(u, breaks = c(-Inf,
          quantile(u, probs = c(0.2, 0.4, 0.6, 0.8, 1), na.rm = TRUE))))))
#[1] 1 1 2 2 3 3 4 4 5 5 1 1 2 2 3 3 4 4 5 5

注意:根据OP提出的quantile问题进行回答

© www.soinside.com 2019 - 2024. All rights reserved.