我计算分位数时只得到1s

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

我有这个数据集:

structure(list(NUMERO = structure(c(10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30), format.stata = "%12.0g"), 
    ingressosmensuals_memb = c(1130, NA, 2700, 1480, 700, 1950, 
    NA, NA, 1907, 451, NA, 1442, NA, 1150, 1200, NA, NA, 1100, 
    650, NA, NA), ingressosmensuals_uc = c(3130, 0, 2700, 2440, 
    700, 1950, 0, 0, 3252, 451, 0, 1442, 0, 1150, 1200, 0, 0, 
    1100, 1844, 0, 0)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -21L), groups = structure(list(
    NUMERO = structure(c(10, 11, 12, 13, 14, 15, 16, 17, 18, 
    19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30), format.stata = "%12.0g"), 
    .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
        10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
        21L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -21L), .drop = TRUE))

其中

ingressosmensuals_uc
计算如下:

Unitatsconsum_2021 <- Unitatsconsum_2021 %>%
  group_by(NUMERO) %>%
  mutate(ingressosmensuals_uc = sum(ingressosmensuals_memb,na.rm=TRUE))

我想用

ingressosmensuals_uc
的 10 分位数创建一个新变量,但我只得到 1s.

到目前为止,我已经尝试过:

Unitatsconsum_2021 <- Unitatsconsum_2021 %>%
  mutate(decils_renda_uc=ntile(ingressosmensuals_uc,10)) %>%
  mutate(decils_renda_uc=factor(decils_renda_uc, levels=c(1,2,3,4,5,6,7,8,9,10),                             labels=c("1r","2n","3r","4t","5è","6è","7è","8è","9è","10è")))
r quantile
1个回答
1
投票

我们可能需要

ungroup
因为它是分组的

library(dplyr)
Unitatsconsum_2021 %>% 
  ungroup %>% 
  mutate(decils_renda_uc=ntile(ingressosmensuals_uc,10)) %>%
  mutate(decils_renda_uc=factor(decils_renda_uc, levels=c(1,2,3,4,5,6,7,8,9,10),                             labels=c("1r","2n","3r","4t","5è","6è","7è","8è","9è","10è")))

-输出

# A tibble: 21 × 4
   NUMERO ingressosmensuals_memb ingressosmensuals_uc decils_renda_uc
    <dbl>                  <dbl>                <dbl> <fct>          
 1     10                   1130                 3130 10è            
 2     11                     NA                    0 1r             
 3     12                   2700                 2700 9è             
 4     13                   1480                 2440 9è             
 5     14                    700                  700 5è             
 6     15                   1950                 1950 8è             
 7     16                     NA                    0 1r             
 8     17                     NA                    0 1r             
 9     18                   1907                 3252 10è            
10     19                    451                  451 5è      
© www.soinside.com 2019 - 2024. All rights reserved.