Explore Courses Blog Tutorials Interview Questions
0 votes
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 (108k 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)]



#[1] "p"


#[1] "o" "t"

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

Browse Categories