Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)
On this string "apple", I want to execute a test to see which character has the most occurring in the string, which is "p".

I have used the str_extract_all("apple"), and turn the list into a tibble, use group_by() and summarise() to return the most occurring character.

I would like to ask if there's an easier way to do this task?

1 Answer

0 votes
by (107k points)

You can easily separate every character and calculate their occurrence using a table and return the one with max frequency.

most_repeated_character <- function(x) {

  tab <- table(strsplit(x, '')[[1]])

  names(tab)[tab == max(tab)]

}

most_repeated_character('apple')

#[1] "p"

most_repeated_character('potato')

#[1] "o" "t"

If you are a beginner and want to know about R then do check out the R programming tutorial

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...