【问题标题】:Creating a table extracting the first letter in a string and counts in R创建一个表格,提取字符串中的第一个字母并在 R 中计数
【发布时间】:2020-02-27 18:08:50
【问题描述】:

我正在尝试提取以逗号分隔的字符串的第一个字母,然后计算该字母出现的次数。因此,我的数据框中的列示例如下所示:

test <- data.frame("Code" =  c("EKST, STFO", "EFGG", "SSGG, RRRR, RRFK", 
"RRRF"))

我想在它旁边添加一个如下所示的列:

test2 <- data.frame("Code" =  c("EKST, STFO", "EFGG", "SSGG, RRRR, RRFK", 
"RRRF"), "Code_Count" = c("E1, S1", "E1", "S1, R2", "R1"))

代码计数列提取字符串的第一个字母并计算该字母在该特定单元格中出现的次数。

我研究过使用 strsplit 来获取以逗号分隔的列中的第一个字母,但我不确定如何将该字母在单元格中出现的次数附加到它。

【问题讨论】:

    标签: r string counting extraction


    【解决方案1】:

    这是使用基数 R 的一个选项。这会在逗号上拆分 Code 列(以及至少一个空格),然后将第一个字母出现的次数制成表格,然后将它们一起粘贴到所需的输出中。它确实按字母顺序对新列进行排序(与您的输出不匹配)。希望这会有所帮助!

    test2$Coode_Count2 <- sapply(strsplit(test2$Code, ",\\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
    } )
    
    test2
                  Code Code_Count Coode_Count2
    1       EKST, STFO     E1, S1       E1, S1
    2             EFGG         E1           E1
    3 SSGG, RRRR, RRFK     S1, R2       R2, S1
    4             RRRF         R1           R1
    

    这是一个更简洁的stringr/purrr 解决方案,它抓取单词的第一个字母并执行相同的操作(而不是拆分字符串)

    library(purrr)
    library(stringr)
    
    map_chr(str_extract_all(test2$Code, "\\b[A-Z]{1}"), function(x) {
      tab <- table(x)
      paste0(names(tab), tab, collapse = ", ")
      } )
    

    数据

    test2 <- data.frame("Code" =  c("EKST, STFO", "EFGG", "SSGG, RRRR, RRFK", 
                                "RRRF"), "Code_Count" = c("E1, S1", "E1", "S1, R2", "R1"))
    test2[] <- lapply(test2, as.character) # factor to character
    

    【讨论】:

    • paste(paste0(names(tab), tab), collapse = ", ") 可以简化为paste0(names(tab), tab, collapse = ", "),对吧?
    猜你喜欢
    • 2014-06-12
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2018-06-27
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多