pssguy
pssguy

Reputation: 3515

How can I use purrr with str_split

Still trying to get to grips with purrr

library(stringr)
library(purrr)

df <- data.frame(text=c("Even Flow", "My Sweet Lord"))

How can I use, presumably, map_chr and str_split to get, say, a vector of the second text elements i.e "Flow" "Sweet"

TIA

Upvotes: 3

Views: 1301

Answers (1)

67342343
67342343

Reputation: 816

Here is a solution with data.table

library(stringr)
library(data.table)

df <- data.table(text=c("Even Flow", "My Sweet Lord"))
df[, text_second := tstrsplit(text, " ")[2]]

and using purrr

library(purrr)
df$text %>% map(str_split, pattern = " ") %>% map_chr(c(1,2))

Upvotes: 4

Related Questions