Reputation: 368
I have numerous variables that are essentially factors that I would like to recode as integers.
A number of variables are a string with the first character being a digit which corresponds with the
integer e.g. 2 = I have considered suicide in the past week, but not made any plans.
should be 2
.
Other variables are yes
or no
and should be 1
or 0
respectively.
Others, have numerous levels based on a number of strings:
none = 0
one = 1
two = 2
three = 3
four or more = 4
Similarly:
ptsd = 0
depression = 1
generalised anxiety = 2
no diagnosis warranted = 3
And:
Female = 0
Male = 1
Other = 2
Some of the values in the cells are NA
and need to remain as NA
. I have tried tried the following code without trying to change all variables (to start simple):
vars1 <- vars(pastpsyc, pastmed, hxsuicide)
vars2 <- vars(siss, mssi_1)
df_rc <- df %>%
## this works
mutate_at(vars1, ~ (case_when(
. == "yes" ~ 1,
. == "no" ~ 0
))) %>%
## this does not
mutate_at(vars2, ~as.integer(str_extract(vars2, "[0-9]"))) %>%
## nor does this
mutate_at(diag1, ~ (case_when(
. == "ptsd" ~ 0,
. == "depression" ~ 1,
. == "generalised anxiety" ~ 2,
. == "no diagnosis warranted" ~ 3
)))
But this fails and I am totally stumped on how to recode the other variables.
How can I change the different strings to the format I need (preferably in a tidy way)? Below is a minimally reproducible dataset.
structure(list(siss = c("2 = I have considered suicide in the past week, but not made any plans.",
"1 = I have had vague thoughts of suicide in the past week.",
"2 = I have considered suicide in the past week, but not made any plans.",
"1 = I have had vague thoughts of suicide in the past week.",
"3 = I have made plans to suicide in the past week, but I haven’t intended to act on these plans."
), mssi_1 = c("1. Weak - unsure about whether he/she wants to die, seldom thinks about death, or intensity seems low.",
"1. Weak - unsure about whether he/she wants to die, seldom thinks about death, or intensity seems low.",
"1. Weak - unsure about whether he/she wants to die, seldom thinks about death, or intensity seems low.",
"1. Weak - unsure about whether he/she wants to die, seldom thinks about death, or intensity seems low.",
"2. Moderate - current desire to die, may be preoccupied with ideas about death, or intensity seems greater than a rating of 1."
), diag1 = c("ptsd", NA, "depression", "generalised anxiety",
"no diagnosis warranted"), pastpsyc = c("yes", NA, "no", NA,
"yes"), pastmed = c("no", "yes", NA, "no", "no"), hxsuicide = c("yes",
NA, "yes", "yes", "yes"), suicide_attempts = c("none", NA, "one",
"two", "four or more"), sex = c("Male", "Other", NA, "Female",
NA)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-5L), spec = structure(list(cols = list(siss = structure(list(), class = c("collector_character",
"collector")), mssi_1 = structure(list(), class = c("collector_character",
"collector")), diag1 = structure(list(), class = c("collector_character",
"collector")), pastpsyc = structure(list(), class = c("collector_character",
"collector")), pastmed = structure(list(), class = c("collector_character",
"collector")), hxsuicide = structure(list(), class = c("collector_character",
"collector")), suicide_attempts = structure(list(), class = c("collector_character",
"collector")), sex = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
Upvotes: 1
Views: 55
Reputation: 30474
This works with very minor changes.
vars2
in str_extract
vars(diag1)
or "diag1"
(or just use mutate
) to change that single columnHope this is helpful.
df %>%
## this works
mutate_at(vars1, ~ (case_when(
. == "yes" ~ 1,
. == "no" ~ 0
))) %>%
## this does not
mutate_at(vars2, ~as.integer(str_extract(., "[0-9]"))) %>%
## nor does this
mutate_at(vars(diag1), ~ (case_when(
. == "ptsd" ~ 0,
. == "depression" ~ 1,
. == "generalised anxiety" ~ 2,
. == "no diagnosis warranted" ~ 3
)))
If you want to use mutate
instead of mutate_at
:
mutate(diag1 = case_when(
diag1 == "ptsd" ~ 0,
diag1 == "depression" ~ 1,
diag1 == "generalised anxiety" ~ 2,
diag1 == "no diagnosis warranted" ~ 3
))
Upvotes: 2