Extract a category string from a column into a column, with tidyverse only. in R

Viewed 53

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")
)
1 Answers

Here is one approach.

First, would pivot_longer your comorbidities, so you have one comorbid condition per row. Then would remove NA and duplicate comorbid conditions.

Then you could use pivot_wider to have columns for each comorbid condition, with 1 if present, and use values_fill for 0 that are absent instead of NA.

library(tidyverse)

test %>%
  pivot_longer(cols = starts_with("Comorbidity"), names_to = "Comorbidity_Count", values_to = "Comorbidity") %>%
  drop_na(Comorbidity) %>%
  select(-Comorbidity_Count) %>%
  distinct() %>%
  mutate(Condition = 1) %>%
  pivot_wider(id_cols = -c(Comorbidity, Condition), names_from = Comorbidity, values_from = Condition, values_fill = list(Condition = 0))

Output

# A tibble: 6 x 19
  ID    Chills Cough  Diarrhoea Fatigue  Headcahe `Loss of smell a… `Muscle Ache` `Nasal Congesti… `Nausea and Vom… `Shortness of B… `Sore Throat` Sputum Temperature `Asthma (manage… `Diabetes Type … `High Blood Pre…  None Obesity
  <chr> <chr>  <chr>  <chr>     <chr>    <chr>    <chr>             <chr>         <chr>            <chr>            <chr>            <chr>         <chr>  <chr>                  <dbl>            <dbl>            <dbl> <dbl>   <dbl>
1 1     No     No     No        No       No       No                No            No               No               No               No            No     No                         1                1                1     0       0
2 2     Mild   Severe Mild      Moderate No       No                Moderate      No               No               Mild             No            Mild   No                         0                0                0     1       0
3 3     No     No     No        Mild     No       No                No            No               No               No               No            No     No                         0                0                1     0       1
4 4     Mild   Mild   No        Mild     Mild     No                Moderate      No               No               No               No            Mild   No                         0                0                1     0       1
5 5     No     Mild   No        Mild     No       No                Mild          Mild             No               No               Mild          Mild   No                         0                0                0     1       0
6 6     No     No     No        Mild     No       No                Mild          No               No               Mild             No            No     37.5-38                    0                0                1     1       0
Related