I am working with air-quality data. I tried to reshape the data frame from wide to long using melt
function. Here is the data: Elev
stands for Elevation
, Obs
for observation
and US3, DK1, DE1
are models, where lm
and ul
represents first and third quantiles.
Elev Obs lm ul US3 lm ul DK1 lm ul
1 0 37.74289 34.33422 41.27840 38.82037 35.35241 42.30042 49.31111 45.00134 53.90968
2 100 38.14076 34.71842 41.36560 39.82727 36.49086 43.22209 50.46545 45.79068 55.44664
3 250 39.31056 35.98180 42.50011 40.94909 37.70768 44.40232 50.79818 45.76405 55.54795
4 500 41.03098 37.78005 44.02544 42.54909 39.25627 45.72927 51.24182 46.76091 55.88568
5 750 43.57307 40.52575 46.92804 43.48000 40.55918 46.62914 51.90364 47.40586 56.37514
DE1 lm ul
1 41.15185 37.81824 44.62509
2 40.89455 37.38491 44.34759
3 40.93455 37.33400 44.32573
4 41.26727 37.90150 44.68568
5 43.04545 40.04541 46.12386
I used
melt(f,id.vars=c("Elev", "lm","um"),measure.vars=c("US3", "DK1", "DE1","Obs" ))
and I got
Elev lm ul variable value
0 34.33422 41.27840 US3 38.82037
100 34.71842 41.36560 US3 39.82727
250 35.98180 42.50011 US3 40.94909
500 37.78005 44.02544 US3 42.54909
750 40.52575 46.92804 US3 43.48000
0 34.33422 41.27840 DK1 49.31111
100 34.71842 41.36560 DK1 50.46545
250 35.98180 42.50011 DK1 50.79818
500 37.78005 44.02544 DK1 51.24182
750 40.52575 46.92804 DK1 51.90364
0 34.33422 41.27840 DE1 41.15185
100 34.71842 41.36560 DE1 40.89455
250 35.98180 42.50011 DE1 40.93455
500 37.78005 44.02544 DE1 41.26727
750 40.52575 46.92804 DE1 43.04545
0 34.33422 41.27840 Obs 37.74289
100 34.71842 41.36560 Obs 38.14076
250 35.98180 42.50011 Obs 39.31056
500 37.78005 44.02544 Obs 41.03098
750 40.52575 46.92804 Obs 43.57307
As it can clearly be seen the values of lm
and ul
are repeated for every elevation. How can I have a long format without the repetition of those values?
My expected result is:
Elev lm ul variable value
0 35.35241 42.30042 US3 38.82037
100 36.49086 43.22209 US3 39.82727
250 37.70768 44.40232 US3 40.94909
500 39.25627 45.72927 US3 42.54909
750 40.55918 46.62914 US3 43.48000
0 45.00134 53.90968 DK1 49.31111
100 45.79068 55.44664 DK1 50.46545
250 45.76405 55.54795 DK1 50.79818
500 46.76091 55.88568 DK1 51.24182
750 47.40586 56.37514 DK1 51.90364
0 37.81824 44.62509 DE1 41.15185
100 37.38491 44.34759 DE1 40.89455
250 37.33400 44.32573 DE1 40.93455
500 37.90150 44.68568 DE1 41.26727
750 40.04541 46.12386 DE1 43.04545
0 34.33422 41.27840 Obs 37.74289
100 34.71842 41.36560 Obs 38.14076
250 35.98180 42.50011 Obs 39.31056
500 37.78005 44.02544 Obs 41.03098
750 40.52575 46.92804 Obs 43.57307