Extract a long word in a string

I would like to find and extract the longest word of a string, if possible, using a package tidyverse.

library(tidyverse)

tbl <- tibble(a=c("ab cde", "bcde f", "cde fg"), b=c("cde", "bcde", "cde"))
tbl
# A tibble: 3 x 1
   a
<chr>
1 ab cde
2 bcde f
3 cde fg

As a result, I am looking for:

# A tibble: 3 x 2
   a     b
  <chr> <chr>
1 ab cde   cde
2 bcde f  bcde
3 cde fg   cde

The closest message to the question I found is this: the longest word in a line . Anyone have an idea for an even simpler way?

+6
source share
2 answers

Solution using the R base:

# Using OPs provided data
tbl$b <- sapply(strsplit(tbl$a, " "), function(x) x[which.max(nchar(x))])

Explanation:

  • Divide each line into words ( strsplit)
  • Determine the word length ( nchar)
  • Choose which word is longer in the string ( which.max)
+13
source

And here is a possible version of @PoGibas answer tidyverse

library(tidyverse)
tbl <- tibble(a=c("ab cde", "bcde f", "cde fg"))

tbl %>% 
  mutate(b = map_chr(strsplit(a, " "), ~ .[which.max(nchar(.))]))

#> # A tibble: 3 x 2
#>        a     b
#>    <chr> <chr>
#> 1 ab cde   cde
#> 2 bcde f  bcde
#> 3 cde fg   cde
+7

All Articles