stsplit and sapply

Hi all

I have some data that looks life this
Phrase Group
this is some text Alpha
some more text Bravo

I need (and I can't find the right combination of strsplit and mutate - sorry if it's right in front of me) is:

Word Group
this Alpha
is Alpha
some Alpha
text Alpha
some Bravo
more Bravo
text Bravo

I can handle the duplicates later. I know strsplit and unlist will give me Word (in the example above) from Phrase. What I need is for it to include the group belonging to each component Word in Phrase (again in the example above).

I think unlist might be the way forward but I can't make it work.

Thanks in advance.

Forgive me replying to my own post but I've just realised it hasn't displayed so well. The first dataset should be

"This is some text", "Alpha"
"some more text", "Bravo"
where the comma separates Phrase from Group.

Looks like tidyr::separate_rows might be what you're looking for

d <- data.frame(Phrase = c('This is some text', 'some more text'),
                Group = c('Alpha', 'Bravo'))

tidyr::separate_rows(d, Phrase)

# A tibble: 7 x 2
  Phrase Group
  <chr>  <chr>
1 This   Alpha
2 is     Alpha
3 some   Alpha
4 text   Alpha
5 some   Bravo
6 more   Bravo
7 text   Bravo

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.