1

This question probably already exists but my biggest issue was in how to phrase the question, so if you could point me to the post with the answer I'd greatly appreciate it!

I have a data frame:

test <- data.frame(A = c("A", "B"), 
                   B = c("This is a long string", "This is another long string"))

  A                           B
1 A       This is a long string
2 B This is another long string

And I'd like the output to still only have two columns, but break the second column up by " " and have the first column repeated so that:

   A       B
1  A    This
2  A      is
3  A       a
4  A    long
5  A  string
6  B    This
7  B      is
8  B another
9  B    long
10 B  string
MayaGans
  • 1,815
  • 9
  • 30
  • Akrun's answer is excellent you can also use ``splitstackshape::cSplit(test, "B", " ", "long")`` if you are not working with ``dplyr`` or the ``tidyr`` packages. – Gainz Feb 13 '20 at 16:28
  • 2
    @MayaGans you don't need to delete a dupe because dupe helps in finding the answer easily while googling – akrun Feb 13 '20 at 18:40

1 Answers1

-1

We can use separate_rows

library(tidyr)
test %>%
   separate_rows(B)
#   A       B
#1  A    This
#2  A      is
#3  A       a
#4  A    long
#5  A  string
#6  B    This
#7  B      is
#8  B another
#9  B    long
#10 B  string

By default, the sep will automatically pick up spaces or other non-alpha numeric characters. If it needs to be specific, then specify the sep argument

akrun
  • 874,273
  • 37
  • 540
  • 662