Home > Software engineering >  how to set a new column with condition
how to set a new column with condition

Time:11-25

the question is I have this column with 1-11 for education. I try to make a new column which can make 1,2 as 1. 3,4,5,6 as 2. 7,8,9,10,11 as 3.

this is my code for dummy, like I get it if is not 1 then 0.

data_3$Relate<-ifelse(data_3$Relation >2,1,0);

but how to deal with the multiple conditon, like more than 1, 0 condition.

data_3$Education<-ifelse(data_3$education<3,'1')

??

CodePudding user response:

A simple way to do it is doing the next:


# create an auxiliar variable
data_3$Education_aux=data_3$education

# change education values based on the auxiliar variable
data_3$education[data_3$Education_aux%in%c(1,2)]=1
data_3$education[data_3$Education_aux%in%c(3,4,5,6)]=2
data_3$education[data_3$Education_aux%in%c(7,8,9,10,11)]=3

# delete the auxiliar variable
data_3$Education_aux=NULL

A more efficient way is using dplyr package:

library(dplyr)
data_3= data_3 %>% mutate(Education=case_when(Education %in% c(1,2) ~ 1,
                                      Education %in% c(3,4,5,6) ~ 2,
                                      Education %in% c(7,8,9,10,11) ~ 3))
                  

CodePudding user response:

There is a bunch of ways to do this. You can index, factor, nest ifelse statements, merge a dictionary, or anything else your heart desires:

set.seed(23)
data_3 <- data.frame(education = sample(1:11, 20, replace = TRUE))

#indexing
data_3$Education_index <- c(rep(1, 2), rep(2, 4), rep(3, 5))[data_3$education]

#facoring
data_3$Education_factor <- as.numeric(cut(data_3$education, breaks = c(0,2,6,11)))

#nested ifelse
data_3$Education_ifelse <- ifelse(data_3$education %in% c(1,2), 1, 
       ifelse(data_3$education %in% 3:6, 2, 3))

#merging dictionary
grades <- data.frame(Education_dictionary = c(rep(1, 2), rep(2, 4), rep(3, 5)),
                     education = 1:11)
data_3 <- merge(data_3, grades, by = "education")


#complicated math
data_3$Education_math <- sapply(ceiling((data_3$education 1)/3) - ` `(data_3$education ==6), \(x) min(c(x,3)))

data_3
#>    education Education_index Education_factor Education_ifelse
#> 1          1               1                1                1
#> 2          1               1                1                1
#> 3          2               1                1                1
#> 4          3               2                2                2
#> 5          3               2                2                2
#> 6          4               2                2                2
#> 7          5               2                2                2
#> 8          6               2                2                2
#> 9          6               2                2                2
#> 10         6               2                2                2
#> 11         7               3                3                3
#> 12         7               3                3                3
#> 13         8               3                3                3
#> 14         8               3                3                3
#> 15         8               3                3                3
#> 16         9               3                3                3
#> 17         9               3                3                3
#> 18        10               3                3                3
#> 19        10               3                3                3
#> 20        11               3                3                3
#>    Education_dictionary Education_math
#> 1                     1              1
#> 2                     1              1
#> 3                     1              1
#> 4                     2              2
#> 5                     2              2
#> 6                     2              2
#> 7                     2              2
#> 8                     2              2
#> 9                     2              2
#> 10                    2              2
#> 11                    3              3
#> 12                    3              3
#> 13                    3              3
#> 14                    3              3
#> 15                    3              3
#> 16                    3              3
#> 17                    3              3
#> 18                    3              3
#> 19                    3              3
#> 20                    3              3
  • Related