how to create a quantile variable using the function svryr::survey_quantile()

Hi there,

I'd like to know how could I create a variable with the values of the quantiles using the svryr::survey_quantile() function.
Here is an example:

library(survey)
library(dplyr)
library(srvyr)

data(api)
dstrata <- apistrat %>%
  as_survey_design(strata = stype, weights = pw)

dstrata %>%
  select(1:2, api99) %>% 
  mutate(quintil = survey_quantile(api99, c(0.2, 0.4, 0.6, 0.8), vartype = NULL))

I was tryng to use the pivot_longer after the mutate to solve this, but, I can't select the coluns created by this function. I don't know why.

I'd like something like this:

dstrata %>%
  select(1:2, api99) %>% 
  mutate(quintil = ntile(api99, 5)) %>% 
  as_tibble()

But, with the survey_quantile function to make sure I'm working rigth with survey.

If is possible, I also would like to have the values not just the number indicating the quantile.

Someone to help?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.