计算字符串的第一个字母并显示它出现的次数,但在 R 中不按字母顺序排列

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

我目前编写了这段代码来计算代码的第一个字母在 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"

当前代码的一切都很完美,除了我希望 R 不按字母顺序输出计数。我希望它保留代码的顺序。所以这就是我希望输出的样子:

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

谢谢!!

这是一个基本的 R 选项,使用 factor 来解决这个问题

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

这给出了

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

tidyverse 的另一个选项。我们可以将 'Code1' 与 separate_rows 拆分,得到 count 并在 arrange 基于频率列的行之后执行 group_by paste

library(dplyr)
library(tidyr)
test %>% 
    separate_rows(Code1) %>%
    mutate(Code1 = substr(Code1, 1, 1)) %>%
    count(State, Code1) %>% 
    arrange(State, n) %>% 
    unite(Code1, Code1, n, sep="") %>% 
    group_by(State) %>% 
    summarise(Code1 = toString(Code1), .groups = 'drop') %>% 
    pull(Code1)
#[1] "A1, E1" "S1"     "A1, S1"