Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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 用分位数在原始df上变异一个新列?_R - Fatal编程技术网

R 用分位数在原始df上变异一个新列?

R 用分位数在原始df上变异一个新列?,r,R,e、 g 是否有一种聪明的“tidyverse”方法或其他方法可以将一个新列变异为每个观察值所属的分位数位于上方的菱形?由于存在多个观察值,我们可以创建一个列表列,然后使用unnest\u 由于存在多个观测值,我们可以创建一个列表列,然后使用unnest\u 通过根据向量的长度对秩进行归一化,可以得到经验分位数: library(dplyr) library(ggplot2) library(tidyr) diamonds %>% mutate(new = list(as.list(

e、 g


是否有一种聪明的“tidyverse”方法或其他方法可以将一个新列变异为每个观察值所属的分位数位于上方的菱形?

由于存在多个观察值,我们可以创建一个列表列,然后使用unnest\u


由于存在多个观测值,我们可以创建一个列表列,然后使用unnest\u


通过根据向量的长度对秩进行归一化,可以得到经验分位数:

library(dplyr)
library(ggplot2)
library(tidyr)
diamonds %>%
   mutate(new = list(as.list(quantile(carat, probs = seq(0.1:1, by = 0.05))))) %>%
    unnest_wider(c(new))
# A tibble: 53,940 x 29
#   carat cut   color clarity depth table price     x     y     z `10%` `15%` `20%` `25%` `30%` `35%` `40%` `45%` `50%` `55%` `60%` `65%`
#   <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 0.23  Ideal E     SI2      61.5    55   326  3.95  3.98  2.43  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 2 0.21  Prem… E     SI1      59.8    61   326  3.89  3.84  2.31  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 3 0.23  Good  E     VS1      56.9    65   327  4.05  4.07  2.31  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 4 0.290 Prem… I     VS2      62.4    58   334  4.2   4.23  2.63  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 5 0.31  Good  J     SI2      63.3    58   335  4.34  4.35  2.75  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 6 0.24  Very… J     VVS2     62.8    57   336  3.94  3.96  2.48  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 7 0.24  Very… I     VVS1     62.3    57   336  3.95  3.98  2.47  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 8 0.26  Very… H     SI1      61.9    55   337  4.07  4.11  2.53  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 9 0.22  Fair  E     VS2      65.1    61   337  3.87  3.78  2.49  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
#10 0.23  Very… H     VS1      59.4    61   338  4     4.05  2.39  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# … with 53,930 more rows, and 7 more variables: `70%` <dbl>, `75%` <dbl>, `80%` <dbl>, `85%` <dbl>, `90%` <dbl>, `95%` <dbl>,
#   `100%` <dbl>

dplyr中还有百分位秩函数,类似,但与上述简单函数不同,它与分位数函数不是完全相反。

您可以通过根据向量长度对秩进行归一化来获得经验分位数:

library(dplyr)
library(ggplot2)
library(tidyr)
diamonds %>%
   mutate(new = list(as.list(quantile(carat, probs = seq(0.1:1, by = 0.05))))) %>%
    unnest_wider(c(new))
# A tibble: 53,940 x 29
#   carat cut   color clarity depth table price     x     y     z `10%` `15%` `20%` `25%` `30%` `35%` `40%` `45%` `50%` `55%` `60%` `65%`
#   <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 0.23  Ideal E     SI2      61.5    55   326  3.95  3.98  2.43  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 2 0.21  Prem… E     SI1      59.8    61   326  3.89  3.84  2.31  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 3 0.23  Good  E     VS1      56.9    65   327  4.05  4.07  2.31  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 4 0.290 Prem… I     VS2      62.4    58   334  4.2   4.23  2.63  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 5 0.31  Good  J     SI2      63.3    58   335  4.34  4.35  2.75  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 6 0.24  Very… J     VVS2     62.8    57   336  3.94  3.96  2.48  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 7 0.24  Very… I     VVS1     62.3    57   336  3.95  3.98  2.47  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 8 0.26  Very… H     SI1      61.9    55   337  4.07  4.11  2.53  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# 9 0.22  Fair  E     VS2      65.1    61   337  3.87  3.78  2.49  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
#10 0.23  Very… H     VS1      59.4    61   338  4     4.05  2.39  0.31  0.32  0.35   0.4  0.42   0.5  0.53   0.6   0.7  0.73   0.9     1
# … with 53,930 more rows, and 7 more variables: `70%` <dbl>, `75%` <dbl>, `80%` <dbl>, `85%` <dbl>, `90%` <dbl>, `95%` <dbl>,
#   `100%` <dbl>
dplyr中也有百分位秩函数,类似,但与上面的简单函数不同,它不是分位数函数的倒数。

您可以使用cut

你可以用cut


由于长度不同,不能有一列具有该输出。我在下面发布了一个解决方案。可能会有帮助。您缺少前两个分幅:0-5%和5-10%。由于长度差异,您不能有一个具有该输出的列。我在下面发布了一个解决方案。可能会有帮助。您缺少前两个分幅:0-5%和5-10%。啊,这会为每个值添加一个新列。我的意思是一个新的列,它包含每行的分位数。啊,它为每一个值添加一个新列。我的意思是一个新的列,包含每行的分位数。
percentile <- function(x) round(100 * (rank(x) - 1)/(length(x) - 1), 2)

diamonds %>% mutate(carat_percentile = percentile(carat))
#> # A tibble: 53,940 x 11
#>    carat cut       color clarity depth table price     x     y     z carat_percentile
#>    <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>            <dbl>
#>  1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43             0.32
#>  2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31             0.03
#>  3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31             0.32
#>  4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63             2.84
#>  5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75             9.88
#>  6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48             0.83
#>  7 0.24  Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47             0.83
#>  8 0.26  Very Good H     SI1      61.9    55   337  4.07  4.11  2.53             1.69
#>  9 0.22  Fair      E     VS2      65.1    61   337  3.87  3.78  2.49             0.04
#> 10 0.23  Very Good H     VS1      59.4    61   338  4     4.05  2.39             0.32
#> # ... with 53,930 more rows
quantile(diamonds$carat, percentile(diamonds$carat)[1:5]/100)
#> 0.32% 0.03% 0.32% 2.84% 9.88% 
#>  0.23  0.21  0.23  0.29  0.31 
data(diamonds, package="ggplot2")
library(dplyr)

diamonds %>%
  mutate(tile5=cut(carat, breaks=quantile(carat, probs=seq(0,1,0.05)), include=T, labels=F)) %>%
  select(carat, tile5)
# A tibble: 53,940 x 2
   carat tile5
   <dbl> <int>
 1 0.23      1
 2 0.21      1
 3 0.23      1
 4 0.290     1
 5 0.31      2
 6 0.24      1
 7 0.24      1
 8 0.26      1
 9 0.22      1
10 0.23      1
# ... with 53,930 more rows