Ordering Likert scales in Cross Tabs

Tagged: 

  • Ordering Likert scales in Cross Tabs

     David updated 1 month, 1 week ago 3 Members · 12 Posts
  • jordan.trachtenberg

    Member
    October 20, 2020 at 8:17 am

    In this cross-tab below, how can we order the Likert scale (Excellent, Vgood, Good, Fair, Poor) instead of having it in ascending alphabetical order?

    nhanes %>% 
    drop_na(gender, health_gen) %>%
    tabyl(gender, health_gen) %>%
    adorn_totals(where = c("row", "col")) %>%
    adorn_percentages() %>%
    adorn_pct_formatting(digits = 1) %>%
    adorn_ns()
  • diego.catalan molina

    Member
    October 20, 2020 at 9:35 am

    Have you tried fct_reorder? See here some examples. In general, I tend to reorder factors (e.g., dichotomous, categorical vars) before pushing them into a table or plot to avoid cleaning variables in the table/plot.

    The most basic way of formatting factors is through the factor() function. E.g., factor(health_gen, levels = c("Poor", "Fair", "Good", "Vgood", "Excellent")).

    • jordan.trachtenberg

      Member
      October 20, 2020 at 10:12 am

      Thanks, @diego-catalan-molina for your suggestions. I’m not sure I understand the concept of factors to apply these solutions correctly. When I try

      nhanes %>%

      drop_na(gender, health_gen) %>%

      factor(health_gen, levels = c("Poor", "Fair", "Good", "Vgood", "Excellent")) %>%

      tabyl(gender, health_gen) %>%

      adorn_totals(where = c("row", "col")) %>%

      adorn_percentages() %>%

      adorn_pct_formatting(digits = 1) %>%

      adorn_ns()

      I get this error:

      Error in factor(., health_gen, levels = c(“Poor”, “Fair”, “Good”, “Vgood”, : object ‘health_gen’ not found

      When I try

      nhanes %>% 
      drop_na(gender, health_gen) %>%
      fct_reorder(health_gen, levels = c("Poor", "Fair", "Good", "Vgood", "Excellent")) %>%
      tabyl(gender, health_gen) %>%
      adorn_totals(where = c("row", "col")) %>%
      adorn_percentages() %>%
      adorn_pct_formatting(digits = 1) %>%
      adorn_ns()

      I get this error:

      Error: f must be a factor (or character vector).

      Is it because I’m starting with the nhanes dataframe?

      • diego.catalan molina

        Member
        October 21, 2020 at 11:11 am

        Sorry @jordan-trachtenberg ! I should have been more clear. This is what you want:

        nhanes %>%

        mutate(health_gen_asfactor = #name of your new var

        factor(health_gen, #original var

        levels = c(“Poor”, “Fair”, “Good”, “Vgood”, “Excellent”)) #order you want to impose on categories

        ) %>% # add the rest of your pipe

        • jordan.trachtenberg

          Member
          October 22, 2020 at 7:47 am

          Thank you, @diego-catalan-molina .

          I got this to work for the cross-tab table:

          nhanes %>%

          mutate(health_gen_asfactor = factor(health_gen,

          levels = c("Poor", "Fair", "Good", "Vgood", "Excellent"))

          ) %>%

          drop_na(gender, health_gen) %>%

          tabyl(gender, health_gen_asfactor) %>%

          adorn_totals(where = c("row", "col")) %>%

          adorn_percentages() %>%

          adorn_pct_formatting(digits = 1) %>%

          adorn_ns()

  • David

    Organizer
    October 20, 2020 at 3:51 pm

    Thanks for jumping in, @diego-catalan-molina !

    @jordan-trachtenberg just to clarify, are you trying to make a plot or reorder in a table (or table-like output)? I ask because the answer differs depending on what you want to do. Factors are for reordering in plots. The arrange() function works well when reordering in tables.

    • jordan.trachtenberg

      Member
      October 21, 2020 at 5:44 am

      For now, I’m trying to order the Likert scale in a table, but I could imagine that I would also need it for a plot in the future. I guess it’s good to know that those preferences require separate functions!

  • David

    Organizer
    October 21, 2020 at 3:09 pm

    I’d love to go over this in the office hours this Friday at 10am Pacific. Any chance you can join so we can discuss there?

  • David

    Organizer
    October 22, 2020 at 7:18 am

    Awesome, thanks!

    • jordan.trachtenberg

      Member
      October 22, 2020 at 8:08 am

      @dgkeyes I think I figured out the plot as well:

      # Set up new data frame with desired Likert scale order

      sleep_by_gender_andhealth <- nhanes %>%

      mutate(ordered_healthgen = factor(health_gen, levels = c("Poor", "Fair", "Good", "Vgood", "Excellent"))) %>%

      group_by(gender, ordered_healthgen) %>%

      drop_na(gender, health_gen) %>%

      summarize(mean_hrs_sleep = round(mean(sleep_hrs_night,

      na.rm = TRUE),

      1))

      # Create visualization with new data frame

      sleep_by_gender_andhealth %>%

      ggplot(mapping = aes(x = ordered_healthgen,

      y = mean_hrs_sleep,

      fill = gender)) +

      geom_bar(stat = "identity") +

      coord_flip()

  • David

    Organizer
    October 22, 2020 at 3:15 pm

    Great!

Log in to reply.

Original Post
0 of 0 posts June 2018
Now
The R for the Rest of Us community is live! Join regular office hours, ask questions in the forum, and more!