elemn
elemn

Reputation: 11

Using R to create a dummy variable by quantile using the survey package. Trouble with findInterval function

I'm currently working with public use microdata that requires survey weighting, thus I've gotten fairly familiar with the survey package and srvyr for summary statistics.

I am trying to figure out a way to create an indicator variable for each observation in a survey object datatable which corresponds to that observation's quantile when using the quantile function. For instance, I might want to create a dummy for each observation according to the observation's quantile when calculating for "height".

I found an example of what I'm trying to do here:

Compute quantiles incorporating Sample Design (Survey package)

When following the steps provided in the answer,

data(api)

dclus1 <- svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
data(api)
a <- svyquantile(~api00, design = dclus1, quantiles = seq(0, 1, by=0.1), method = "linear", ties="rounded")

dclus1 <- update( dclus1 , qtile = factor( findInterval( api00 , a ) ) )

I get this returned :

Error in findInterval(api00, a) : 
  'list' object cannot be coerced to type 'double'

I cannot seem to get findInterval to work on the survey quantile list object generated. From documentation for findInterval, it seems it wants a numeric object or vector. Is there a way to ammend this answer provided to get what I need? Am I missing something ?

Upvotes: 1

Views: 91

Answers (1)

elemn
elemn

Reputation: 11

Further to @juan-c 's cmomment, I decided to pull out the quantile information I calculated via

j = a$[1:4]

In order to do :

dclus1 <- update( dclus1 , qtile = factor( findInterval( api00 , j) ) )

Which solved my problem.

Upvotes: 0

Related Questions