我有一个如下所示的数据集:
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
非常感谢您的回答。非常感谢!!
最佳答案
stringr
包通过 str_count
执行此操作。
stringr::str_count(s, "car")
# [1] 3 0 0 1
关于r - GREP(或类似函数)确定 R 中字符串内部和字符串之间的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55814167/