【问题标题】:GREP (or similar function) to determine frequency within and between string in R [duplicate]GREP(或类似函数)确定R中字符串内和字符串之间的频率[重复]
【发布时间】:2019-04-23 15:04:25
【问题描述】:

我有一个如下所示的数据集:

s <- c("car, car, car, toy, toy", "toy", "horse, horse", "car, horse")

如果我grep("car", s) 我将能够得到[1] 1 4

如果我想得到频率,我可以length(grep("car", s))

但是,我想知道是否有可能得到一个结果,该结果也会返回元素内和元素之间的模式频率?

例如预期结果将类似于以下内容:

[1] 1 1 1 4

和/或

$`car, car, car, toy, toy`
[1] 3

$toy
[1] 0

$`horse, hores`
[1] 0

$`car, horse`
[1] 1

和/或

$`car, car, car, toy, toy`
[1] 3

$`car, horse`
[1] 1

和/或

[[1]]
[1] 3

[[4]]
[1] 1

感谢您的回答。非常感谢!!

【问题讨论】:

    标签: r


    【解决方案1】:

    stringr 包使用 str_count 执行此操作。

    stringr::str_count(s, "car")
    # [1] 3 0 0 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      相关资源
      最近更新 更多