Prometheus
Prometheus

Reputation: 2017

String count in R

I am trying to construct a function similar to str_count.

fruit <- c("apple", "banana", "pear", "pineapple")
str_count(fruit, "a")

But rather than specifing the letter, the function should be able count the max number of duplicated characters.

So the output should be:

 Input    Output
 apple       2
 banana      3
  pear       0

I found an answer here. but its written in c++.

Any ideas how I can do this in R?

Upvotes: 1

Views: 226

Answers (1)

akrun
akrun

Reputation: 887951

We split the 'fruit' into characters, check if there are any duplicates then get the max of the frequency or else return 0

sapply(strsplit(fruit, ""), function(x) if(anyDuplicated(x)) max(table(x)) else 0)
#[1] 2 3 0 3

Or slightly more compact

sapply(strsplit(fruit, ""), function(x) (anyDuplicated(x)!=0) * max(table(x)))

Upvotes: 2

Related Questions