计算R中列中字符串出现次数

时间:2017-05-30 10:34:31

标签: r countif

我有一个包含不同字符串的列,这些字符串沿列重复。我想创建另一个列,它基本上总结字符串的出现次数,并用数字填充此列。这相当于在excel中使用countif函数。

1 个答案:

答案 0 :(得分:0)

例如:

s <- c("aaa","aab","aba","abb","bbb")
look_for_what <- "aa"
length(s[grep(look_for_what,s)])
# 2
look_for_what <- "a"
length(s[grep(look_for_what,s)])
# 4