I have a dataset with two variables in a wide format that I want to convert to long. I've seen the examples from the cook for R but they are for just one variable.
My dataset (graph.dat) looks like this:
PH25 PH50 PH75 PH100 P25 P50 P75 P100 r_b c
3.96 5.94 5.94 5.94 1,694 1,736 1,329 800 0.029 7
2.86 2.86 2.86 2.86 1,665 1,792 1,639 1,335 0.027 7
3.96 5.72 5.72 5.72 1,580 1,560 1,156 663 0.023 7
I want to plot PH (x-axis) versus P(y-axis). So for a PH25 value corresponds a value of P25 (and a value of r_b and c); so the long format will be:
PH P r_b c
3.96 1,694 0.029 7
2.86 1,665 0.027 7
3.96 1,580 0.023 7
5.94 1,736 0.029 7
2.86 1,792 0.027 7
5.72 1,560 0.023 7
5.94 1,329 0.029 7
2.86 1,639 0.027 7
5.72 1,156 0.023 7
5.94 800 0.029 7
2.86 1,335 0.027 7
5.72 663 0.023 7
I try to "melt" it twice but it doesn't work. Here is what I did:
graph.dat1<- melt(graph.dat, id.vars=c("PH25","PH50","PH75",
"PH100","r_b", "c"),
variable.name="P",
value.name="Pval")
and the new dataset (graph.dat1) is fine because I've only three values for each "P" (giving me a total of 12 observations). It looks like this:
PH25 PH50 PH75 PH100 r_b c P Pval
1 3.96 5.94 5.94 5.94 0.029 7 P25 1694
2 2.86 2.86 2.86 2.86 0.027 7 P25 1665
3 3.96 5.72 5.72 5.72 0.023 7 P25 1580
4 3.96 5.94 5.94 5.94 0.029 7 P50 1736
5 2.86 2.86 2.86 2.86 0.027 7 P50 1792
6 3.96 5.72 5.72 5.72 0.023 7 P50 1560
7 3.96 5.94 5.94 5.94 0.029 7 P75 1329
8 2.86 2.86 2.86 2.86 0.027 7 P75 1639
9 3.96 5.72 5.72 5.72 0.023 7 P75 1156
10 3.96 5.94 5.94 5.94 0.029 7 P100 800
11 2.86 2.86 2.86 2.86 0.027 7 P100 1335
12 3.96 5.72 5.72 5.72 0.023 7 P100 663
but when I do the second "melt" it doesn't work and I don't know how to solve it. This is the second step I did:
graph.dat2<- melt (graph.dat1,id.vars=c("r_b", "c", "P", "Pval"),
variable.name="PdH",
value.name="PH")
but then I get 4 times the number of observations (so instead of 12 observations I get 48 observations). So, my new dataset (graph.dat2) looks like:
r_b c P Pval PdH PH
1 0.029 7 P25 1694 PH25 3.96
2 0.027 7 P25 1665 PH25 2.86
3 0.023 7 P25 1580 PH25 3.96
4 0.029 7 P50 1736 PH25 3.96
5 0.027 7 P50 1792 PH25 2.86
6 0.023 7 P50 1560 PH25 3.96
7 0.029 7 P75 1329 PH25 3.96
8 0.027 7 P75 1639 PH25 2.86
9 0.023 7 P75 1156 PH25 3.96
10 0.029 7 P100 800 PH25 3.96
11 0.027 7 P100 1335 PH25 2.86
12 0.023 7 P100 663 PH25 3.96
13 0.029 7 P25 1694 PH50 5.94
14 0.027 7 P25 1665 PH50 2.86
15 0.023 7 P25 1580 PH50 5.72
...