Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何将字符串转换为聚合分位数?_R_Aggregate_Quantile_Percentile - Fatal编程技术网

R 如何将字符串转换为聚合分位数?

R 如何将字符串转换为聚合分位数?,r,aggregate,quantile,percentile,R,Aggregate,Quantile,Percentile,我有一个按组嵌套的数据帧。我想将变量“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",

我有一个按组嵌套的数据帧。我想将变量“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在分组的data.frame上,您可以使用
dplyr::ntile()

库(dplyr)
df%>%
组别(y)%>%
突变(z=ntile(x,5))
#一个tibble:20x3
#分组:y[2]
x y z
1 A 1
2 5 A 1
321A2
4 24 A 2
5 43A 3
6 47 A 3
7 56 A 4
8 59 A 4
9 68 A 5
10 69 A 5
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
2067B5

我们可以使用
cut
breaks
作为
分位数

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

注意:根据
分位数
问题回答OP提问

回答得很好,谢谢。快速提问,如何将
z
列提取为
df
中的新列?@MarcoPastorMayo-作为管道的一部分,您可以执行:
..%>%拉(z)
。另一个注释。
seq_-along()
函数对于我需要做的事情是不必要的(它实际上使它无法工作)
ntile()
足以获得与观测值匹配的字符串。@MarcoPastorMayo-对,我误解了您提到的位置,向量已经排序。我刚刚意识到这个答案实际上给出了整个向量的分位数,而不是组分位数。假设数据是
df
df$z <- c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5)
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
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
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