I have a data frame (df) read in with read.csv which looks like this:
Reaction GID
1 A1 11
2 A2 21 / 22 / 23 / 24
3 A3 31 / 32
4 A4 41
5 A5 51 / 52 / 53
The data frame with column header has 2 columns and n rows, but the "GID" columns has strings with several entries separated by "/". As you can see, the strings have not the same length in every row. I want the columns separated by each "/"-entry and then melted to long format.
Thus it should look like:
Reaction GID
1 A1 11
2 A2 21
3 A2 22
4 A2 23
5 A2 24
6 A3 31
and so on. I first applied the code from here: Multiple Separators for the same file input R
df2 <-do.call(rbind.data.frame,strsplit(df$GID," / "))
However, the lines, which have only one entry in "GID" also get multiplicated, where the duplicate entries should be left out.
GID
1 11, 11, 11, 11
2 21, 22, 23, 24
3 31, 32, 31, 32
Thus, this approach recycles the entries and also the first "Reaction" column with the identifiers was left out, making a merging or matching back impossible.
This does not look like the correct way for me. What would be the correct method to reach my goal?