【问题标题】:Calculate frequency of all possible combinations of substrings in a vector计算向量中所有可能的子串组合的频率
【发布时间】:2021-01-29 16:22:18
【问题描述】:

我有一个像strings 这样的向量,并且想要计算由分隔符“|”分隔的每个值的频率以及它们的组合,例如 R 中的result

strings <- c('a', 'a|b', 'a|c', 'a|b|c|d')

# Calculate how many times 'a' is present, how many times 'a' and 'b', denoted 'ab', are present, etc. My goal is to be able to identify which combinations of substrings are most common.

result <- data.frame(substring = c('a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'bc', 'bd', 'abc', 'abd', 'abcd'),
                     frequency = c(1, .5, .5, .25, .5, .5, .25, .25, .25, .25, .25, .25))

【问题讨论】:

    标签: r string combinations frequency


    【解决方案1】:

    首先,获取给定集合的每个子集称为power set

    有一个包rje 包含函数powerSet,它为给定的向量生成这个。

    下面我们有一个函数,它将一个向量(假设它具有由 OP 指示的形式(即管道分隔和小写字母))作为输入,生成幂集,最后确定频率每个子字符串。

    library(rje)
    
    getFreqs <- function(v) {
        idx_lets <- Reduce(union, sapply(strsplit(v, ""), function(x) {
            match(x, letters, nomatch = 0)
        }))
        
        do.call(rbind, lapply(powerSet(letters[idx_lets])[-1], function(x) {
            data.frame(substring = paste(x, collapse = ""),
                       frequency = ifelse(length(x) > 1,
                                          length(Reduce(intersect, sapply(x, function(y) {
                                               which(grepl(y, v))
                                           }))),
                                          sum(grepl(x, v))) / length(v)
            )
        }))
    }
    

    这是一个例子:

    getFreqs(strings)
       substring frequency
    1          a      1.00
    2          b      0.50
    3         ab      0.50
    4          c      0.50
    5         ac      0.50
    6         bc      0.00
    7        abc      0.25
    8          d      0.25
    9         ad      0.25
    10        bd      0.25
    11       abd      0.25
    12        cd      0.25
    13       acd      0.25
    14       bcd      0.25
    15      abcd      0.25
    

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多