rggplot2dplyrtidyrjanitor

Creating a col that adds a numeric value based on the order of a character string in R


I have responses where I solicited rankings of issues and the response I have for each individual look like this:

structure(list(Rank = "Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);Economic impacts;Climate change;"), row.names = c(NA, 
-1L), class = "data.frame")

I am trying to create a column that gives the numeric value of each Rank based on the order of each string. I would want five columns for each individual string separated by ";" that would look like this:

Shifting angler preferences and behaviors 
5

Increasing effort and fishing power
4

R3 (Recruitment, Retention, and Reactivation)
3

Economic impacts
2

Climate change
1

This is just a single response where we solicited the rankings of the issues and I am trying to visualize the responses.

Thanks!


Solution

  • Just a guess, I think you want to know the order in which each of those ;-delimited substrings appears in the vector, where first is "highest priority (and therefore 5), etc. I'll assume that the substrings will never change.

    fun <- function(x, table = NA) {
      if (anyNA(table)) {
        # obtained in the question, assuming that these 
        table <- trimws(strsplit("Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);Economic impacts;Climate change", ";")[[1]])
      }
      bind_rows(
        lapply(strsplit(x, ";"),
               function(z) setNames(length(table) + 1L - match(table, trimws(z)), table))
      )
    }
    

    Demonstration, using augmented data:

    quux <- structure(list(Rank = c("Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);Economic impacts;Climate change;", "Economic impacts;Climate change;Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);")), class = "data.frame", row.names = c(NA, -2L))
    
    out <- quux %>%
      mutate(fun(Rank))
    str(out)
    # 'data.frame': 2 obs. of  6 variables:
    #  $ Rank                                         : chr  "Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and R"| __truncated__ "Economic impacts;Climate change;Shifting angler preferences and behaviors;Increasing effort and fishing power;R"| __truncated__
    #  $ Shifting angler preferences and behaviors    : int  5 3
    #  $ Increasing effort and fishing power          : int  4 2
    #  $ R3 (Recruitment, Retention, and Reactivation): int  3 1
    #  $ Economic impacts                             : int  2 5
    #  $ Climate change                               : int  1 4
    out
    #                                                                                                                                                           Rank Shifting angler preferences and behaviors Increasing effort and fishing power R3 (Recruitment, Retention, and Reactivation) Economic impacts Climate change
    # 1 Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);Economic impacts;Climate change;                                         5                                   4                                             3                2              1
    # 2 Economic impacts;Climate change;Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);                                         3                                   2                                             1                5              4