Expanding/Duplicating/Tidying a row with an element containing two or more values?

53 Views Asked by At

Dear StackOverflow community,

I am struggling to convert my dataset into tidy format. I have a dataframe with some columns containing values with "multiple values". These multiple values are given as a string with a constant separator ("|"). An example of my dataset is given below:

df <- tibble(
v1 = c(1,2,3),
v2 = c('x','y','z'),
v3 = c('a|b','c|d|e','f'),
v4 = c('red|blue','green|yellow|pink','purple'))
df

My desire is to turn it into tidy format by "expanding"/"duplicating" the rows containing multiple values, where constant values are duplicated and the values between the constant separator will be the "new value" in the new rows across all columns that contain multiple values. I.e., my desired output would look like so:

df_desired <- tibble(
v1 = c(1,1,2,2,2,3),
v2 = c('x','x','y','y','y','z'),
v3 = c('a','b','c','d','e','f'),
v4 = c('red','blue','green','yellow','pink','purple'))

df_desired

Rows that already are tidy should stay constant.

Quick illustration of the process that I would like to do

I apologize for poor description of my problem, but I hope the examples I have given communicates my problem and desired result. Thank you all in advance!

0

There are 0 best solutions below