Skip to content

Factors/levels + dplyr::arrange() #54

Description

@ariannaa7

In this situation, we needed to display rows in a customized order based on the variable_level (med names) column.

Here, I created multiple vectors based on the category of medication and set the desired order of the medications within the vectors. I then mutated the variable_name column as a factor and specified the vectors as the level (again in the desired order).

# Vectors with med names in the order in which they should appear for that category of meds
med_group_A <- c("med_A2", "med_A4", "med_A1", "med_A3")
med_group_B <- c("med_B5", "med_B4", "med_B2", "med_B3", "med_B1")
med_group_C <- c("med_C1", "med_C3", "med_C4", "med_C2")

# Vectors for the order in which rows of variable_name column should appear
variable_name_levels <- c("example1", "example2", "example3", "example4")

meds_summarised_result <- meds_summarised_result %>%
  omopgenerics::splitGroup() %>%
      dplyr::mutate(
        variable_name = factor(
          variable_name,
          levels = variable_name_levels
        ),
        variable_level = factor(
          variable_level,
          levels = c(med_group_B, med_group_A, med_group_C,) # we first want all the meds in groupB, then groupA, then groupC so we specify this with the order in which we list the vectors
        )
      ) %>%
      dplyr::arrange(cdm_name, strata_name, variable_name, variable_level) %>% # arrange from zoomed out to zoomed in
      dplyr::mutate(variable_name = as.character(variable_name), # remove factors
                    variable_level = as.character(variable_level)) %>%
      omopgenerics::uniteGroup(cols = c("cohort_name")) # unite once more
 

Using omopgenerics::splitGroup() and omopgenerics::uniteGroup maintains the integrity of the summarised_results object! Attempting to arrange without it or with just "variable_level" (instead cdm_name, strata_name, ..., variable_level) for example, completely reformatted the table.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions