Assume I have the following database df.
df <- data.frame(ID= c("A", "B", "C"),
Var1 = c(234, 12, 345),
Var2 = c(4, 555, 325),
Var3 = c("45|221|2", "982", NA))
> df
ID Var1 Var2 Var3
1 A 234 4 45|221|2
2 B 12 555 982
3 C 345 325 <NA>
I would like to create a data.frame in which Var1 and Var2 is combined with the elements in Var3 by ID.
The outcome I am looking for should look like the following:
> outcome
ID VarA VarB
1 A 234 45
2 A 234 221
3 A 234 2
4 A 4 45
5 A 4 221
6 A 4 2
7 B 12 982
8 B 555 982
Note that:
- the elements in
Var3are separated by a vertical bar| ID == Cis not inoutcomebecauseVar3isNAfor thatID.
The original data consists of millions of IDs.