Function in R that returns first word in a sentence that is having a length which is an even number & also longest even word

You can do this using the popular stringr and dplyr libraries.

library(dplyr)
library(stringr)

df <- tibble(
  sentence = c(
    "Time & tide waits for none",
    " Tit for tat",
    "Eyes are mirror of person's thoughts",
    "Some Other Sentence",
    "Odd sentences failure"
  )
)

df <- df %>%
  # Split the sentence and store it in a new column
  mutate(split_sentence = str_split(sentence," ")) %>%
  # Do the next step row wise because we will be dealing with a vector of vectors
  rowwise() %>%
  # Keep only words that have a remainder of 0 when divided by 2 (str_length modulo 2)
  mutate(split_sentence = list(split_sentence[str_length(split_sentence) %% 2 == 0])) %>%
  # Only keep non-null strings !""
  mutate(split_sentence = list(split_sentence[str_length(split_sentence) > 0])) %>%
  # Find the first word with the longest length
  mutate(split_sentence = list(split_sentence[which.max(str_length(split_sentence))])) %>%
  # Keep only the first word left in the vector or return NA if none left
  mutate(first_even = first(split_sentence)) %>%
  # Ungroup because we don't need to work rowwise anymore
  ungroup() %>%
  # Convert any NA values to "00" per question
  mutate(first_even = ifelse(is.na(first_even),"00",first_even)) %>%
  select(-split_sentence)

# A tibble: 5 x 2
#   sentence                             first_even
#   <chr>                                <chr>     
# 1 Time & tide waits for none           Time      
# 2 " Tit for tat"                       00        
# 3 Eyes are mirror of person's thoughts person's  
# 4 Some Other Sentence                  Sentence  
# 5 Odd sentences failure                00       

In your description you said that thoughts would be the longest word but my alogrithm found that person's was just as long. If you want to remove the appostrophe you can figure out how to do that using the str_remove_all() function. I’ll leave that for you.

Leave a Comment