Linked Questions

Popular Questions

Make a function that groups new variables

Asked by At

I have this kind of dataset;

dt <- data.table(ID = c(1, 2, 3, 4),
                 q1= c(1, NA, NA, 1), 
                 q2= c(1, 3, 2, NA), 
                 q3= c(2, 1, 4, 4))

I need to make new variables based on q1, q2, q3, but i want to group the values; so its should be that all with value 1, 2 = YES, all with value 3 = NO, all with value 4 = IDK

So final dataset should be

ID q1 q2 q3 q1_cat q2_cat q3_cat
1  1  1  2   YES     YES     YES     
2  NA 3  1   NA      NO      YES 
3  NA 2  4  NA       NO      IDK
4  1 NA  4  YES      NA      IDK


        

Related Questions