Farid Abu Bakr
Farid Abu Bakr

Reputation: 1

transform 3 categorical variables into 1 dummy with mutate and ifelse functions

I have data from a social survey. One of the categorical variables is education. It breaks down the population into 6 groups: "1" for those who have primary education, "2" for those who have O level, "3" for those who have complee secondary education. 4 for bachelors' degree. 5 for master's, 6 for PhD.

   wage age sex edu area satisf
1    NA  76   2   6    1   <NA>
2 17000  26   2   6    1      1
3    NA  74   1   6    1   <NA>
4    NA  73   2   6    1   <NA>
5    NA  49   2   5    1      1
6 25000  31   1   6    1      2

I have previously run a regression and decided that I don't need categories 1-3. SO I would like to have the following structure:

DUMMY1(edu)={█(1,&edu="1" ,2,3@0,&edu=else)┤

DUMMY2(edu)={█(1,&edu="4" @0,&edu=else)┤

DUMMY3(edu)={█(1,&edu="5" @0,&edu=else)┤

DUMMY4(edu)={█(1,&edu="6" @0,&edu=else)┤

I need to learn how to use mutate and ifelse functions. My current command is the following:

vova5 <- mutate(vova4,bedu=ifelse(vova4$edu<=3,vova4$edu2 <- 1,
                                   vova4$edu2 <- vova4$edu-2))

but it does not seem to work.

Upvotes: 0

Views: 179

Answers (1)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6769

Without your data, I would try case_when, something like:

Data sample:

vova4 <- data.frame(
  edu = c(1, 2, 3, 4, 4, 5, 5, 6, 6), 
  age = c(70, 56, 66, 67, 34, 55, 33, 44, 32))

Try this:

library(tidyverse)
vova5 <- vova4 %>% 
  mutate(Bedu = case_when(edu<=3 ~ 1, 
         edu==4 ~ 2, 
         edu==5 ~ 3, 
         TRUE ~ 4)) 

vova5

Or:

vova5 <- vova4 %>% 
  mutate(Bedu = case_when(edu<=3 ~ 1, 
         edu==4 ~ 2, 
         edu==5 ~ 3, 
         edu==6 ~ 4)) 

You will get:

> vova5
  edu age Bedu
1   1  70    1
2   2  56    1
3   3  66    1
4   4  67    2
5   4  34    2
6   5  55    3
7   5  33    3
8   6  44    4
9   6  32    4

Upvotes: 0

Related Questions