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!