rstringcountstrsplitpreserve

Counting first letter of string and showing how many times it appears, but not in alphabetical order in R


I currently have this code written to count how many times the first letter of a code appears in a specific column in a table.

#a test data frame    
test <- data.frame("State" = c("PA", "RI", "SC"), "Code1" = c("EFGG, AFGG", "SSAG", "AFGG, SSAG"))

#code to count method codes
test[] <- lapply(test, as.character)


test_counts <- sapply(strsplit(test$Code1, ",\\s+"), function(x) {
  tab <- table(substr(x, 1, 1)) # Create a table of the first letters
  paste0(names(tab), tab, collapse = ", ") # Paste together the letter w/ the number and collapse 
them
} )

#example of output
[1] "A1, E1" "S1"     "A1, S1"

Everything about this current code is perfect, except I would like R to not output the counts in alphabetical order. I would like it to preserve the order of codes. So this is how I would like the output to look:

 [1] "E1, A1", "S1", "A1, S1"

Thank you!!


Solution

  • Here is a base R option using factor to address the issue

    sapply(
      strsplit(test$Code1, ", "),
      function(x) {
        toString(
          do.call(
            paste0,
            rev(stack(table(factor(u<-substr(x, 1, 1),levels = unique(u)))))
          )
        )
      }
    )
    

    which gives

    [1] "E1, A1" "S1"     "A1, S1"