1

I have a column in my dataset that needs to be unnested but the catch is that only certain rows need to be unnested.

I've assigned id numbers to each row and gotten rid of all the , and ; such as:

df %>%
  mutate(id = row_number(), Name=strsplit(Name, [ ,;]+"))

Here is an example:

Name                                              id
John                                              1
John Macrae                                       2
Jason Thomas; Jason Tomas, Jason Timas            3
Jonothan Jamie;  Jonathan Matthew                 4

My desired output would be

Name                                         Id
John                                         1
John Macrae                                  2
Jason Thomas                                 3
Jason Tomas                                  3
Jason Timas                                  3
Jonothan Jamie                               4
Jonathan Matthew                             4
df %>%
  mutate(id = row_number(), Name=strsplit(Name, [ ,;]+")) %>%
  unnest (cols= Name)

But unfortunately, this unnests every single value in the column which I do not want. Is there a way to choose specific rows to unnest using a by or where function in the unnest line?

Thanks everyone for your help!

MSR
  • 2,731
  • 1
  • 14
  • 24
Dr.E77
  • 107
  • 6

1 Answers1

1

You can use separate_rows() from tidyr:

separate_rows(df, Name, sep = "[;,]")

                Name id
1               John  1
2        John Macrae  2
3       Jason Thomas  3
4        Jason Tomas  3
5        Jason Timas  3
6     Jonothan Jamie  4
7   Jonathan Matthew  4
tmfmnk
  • 38,881
  • 4
  • 47
  • 67