I am trying to get new columns, only with tidyverse library based on the categories (strings) found in Comorbitidy_one, Comorbidity_two, Comorbitidy_3 etc. I am intending to use the new columns for a Logistic Regression. Thus, the new columns, named after the strings in the columns should be binary (0 and 1). 0 being absent and 1 being present. For example Comorbidity_one has "Asthma (managed with an inhaler)" yet, it may or may not appear in the next ones, therefore the "Asthma (managed with an inhaler)" becomes a new column with 1 for patients having this morbidity and 0 for those who don't. But I may have Obesity in Comorbidity_two , yet, this becomes a new column with 1 for patients dealing with obesity. Etc.
This is the type of table I have:
test <- structure(
list(
ID = c("1",
"2", "3",
"4", "5",
"6"),
Chills = c("No", "Mild", "No", "Mild", "No", "No"),
Cough = c("No", "Severe", "No", "Mild", "Mild", "No"),
Diarrhoea = c("No", "Mild", "No", "No", "No", "No"),
Fatigue = c("No", "Moderate", "Mild", "Mild", "Mild", "Mild"),
Headcahe = c("No", "No", "No", "Mild", "No", "No"),
`Loss of smell and taste` = c("No", "No", "No", "No", "No", "No"),
`Muscle Ache` = c("No", "Moderate", "No", "Moderate", "Mild", "Mild"),
`Nasal Congestion` = c("No", "No", "No", "No", "Mild", "No"),
`Nausea and Vomiting` = c("No", "No",
"No", "No", "No", "No"),
`Shortness of Breath` = c("No",
"Mild", "No", "No", "No", "Mild"),
`Sore Throat` = c("No",
"No", "No", "No", "Mild", "No"),
Sputum = c("No", "Mild",
"No", "Mild", "Mild", "No"),
Temperature = c("No", "No",
"No", "No", "No", "37.5-38"),
Comorbidity_one = c(
"Asthma (managed with an inhaler)",
"None",
"Obesity",
"High Blood Pressure (hypertension)",
"None",
"None"
),
Comorbidity_two = c("Diabetes Type 2", NA,
NA, "Obesity", NA, NA),
Comorbidity_three = c(
"Asthma (managed with an inhaler)",
"None",
"Obesity",
"High Blood Pressure (hypertension)",
"None",
NA_character_
),
Comorbidity_four = c(
"Asthma (managed with an inhaler)",
"None",
"High Blood Pressure (hypertension)",
NA_character_,
NA_character_,
NA_character_
),
Comorbidity_five = c(
"Asthma (managed with an inhaler)",
"None",
NA_character_,
NA_character_,
NA_character_,
NA_character_
),
Comorbidity_six = c(
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_
),
Comorbidity_seven = c(
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_
),
Comorbidity_eight = c(
"High Blood Pressure (hypertension)",
NA_character_,
NA_character_,
NA_character_,
NA_character_,
NA_character_
),
Comorbidity_nine = c(
NA_character_,
NA_character_,
NA_character_,
"High Blood Pressure (hypertension)",
NA_character_,
"High Blood Pressure (hypertension)"
)
),
row.names = c(NA,-6L),
class = c("tbl_df",
"tbl", "data.frame")
)