Reputation: 39
I have a dataset called brmayors
. Within this dataset, there is a variable called DESCRICAO_GRAU_INSTRUCAO
.
I want to create a new variable called collegedegree
, which is yes when the variable DESCRICAO_GRAU_INSTRUCAO
shows SUPERIOR COMPLETO
and no otherwise.
To create this new variable, I am trying to run brmayors$collegedegree <- ifelse(brmayors$DESCRICAO_GRAU_INSTRUCAO == "SUPERIOR COMPLETO")
I am trying to use function ifelse
but I do not know how to manage it.
Could someone please help me?
Thanks a lot!
Upvotes: 0
Views: 55
Reputation: 886948
In base R
, we can do this without any if/else
or ifelse
brmayors$collegedegree <- c("no", "yes")[1+
(brmayors$DESCRICAO_GRAU_INSTRUCAO == "SUPERIOR COMPLETO")]
Also, it is better to keep as logical
instead of yes/no
because filtering is much easier with that
brmayors$collegedegree <- brmayors$DESCRICAO_GRAU_INSTRUCAO == "SUPERIOR COMPLETO"
Upvotes: 0
Reputation: 388817
You can use ifelse
like :
brmayors$collegedegree <- ifelse(brmayors$DESCRICAO_GRAU_INSTRUCAO == "SUPERIOR COMPLETO", "yes", "no")
Or with case_when from dplyr
:
library(dplyr)
brmayors %>%
mutate(collegedegree =
case_when(DESCRICAO_GRAU_INSTRUCAO == "SUPERIOR COMPLETO" ~ 'yes',
TRUE~'no'))
Upvotes: 1