Extract longest word in string

旧城冷巷雨未停 提交于 2019-12-04 05:30:39

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)

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!