Search code examples
rvariablesbinarynumericlogistic-regression

Changing numeric variable to categorical variable?


I am a finance student and have been playing around in R the past couple of weeks (Rookie here..).
QUESTION: I have two numeric variables: A and B. And I want turn these in one cathegorical variable C. C takes the following values:
1 if A and B both score top decile – or quintile of the distribution.
0 otherwise

Does anyone have any idea how to effectuate this? Thank you in advance!


Solution

  • If I get your meaning right, You can Use this function:

        get_categorical = function(A,B,decile=9){
          da = as.numeric(quantile(A,probs=seq(0.1,0.9,by=0.1)))[decile];
          db = as.numeric(quantile(B,probs=seq(0.1,0.9,by=0.1)))[decile];
          categ = ifelse(A>=da & B>db,1,0);
          return (categ);
        }
    

    Now you can set A and B as arguments:

    get_categorical(A,B)
    

    Hope it helps.