Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I only get 1s when I calculate quantiles

I have this dataset:

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))

where ingressosmensuals_uc is calculated as follows:

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

I want to create a new variable with the quantiles by 10 of ingressosmensuals_uc but I only get 1s.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So far, I’ve tried:

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è")))

>Solution :

We may need to ungroup as it is grouped

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è")))

-output

# 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è      
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading