Suppose I have the following data frame
> df <- data.frame(var1 = c("A", "B", "C", "D"),
var2 = c("test", "5 | 6", "X & Y", "M | N | O"))
> df
var1 var2
1 A test
2 B 5 | 6
3 C X & Y
4 D M | N | O
How can I split the values in var2 by the | and the & operator and put them as separate rows into the same data.frame. The output should look like the following:
> df
var1 var2
1 A test
2 B 5
3 B 6
4 C X
5 C Y
6 D M
7 D N
8 D O
I used strsplit and a for loop to achieve it. However, I think that this is not very well coded. Any ideas how to achieve this in a better R-way?
You could do something like this: