moabit21
moabit21

Reputation: 659

Extract longest word in string

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

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

The result I am looking for is:

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

The closest post to the question I have found is this: longest word in a string. Does anyone have an idea for an even simpler way?

Upvotes: 8

Views: 4248

Answers (2)

markdly
markdly

Reputation: 4534

And here is a possible tidyverse version of @PoGibas's answer

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

Upvotes: 8

pogibas
pogibas

Reputation: 28369

Solution using base R:

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

Explanation:

  • Split each line into words (strsplit)
  • Determine word length (nchar)
  • Select which word is longest in line (which.max)

Upvotes: 18

Related Questions