I have data as follows:
DT <- structure(list(ECOST = c("Choice_01", "Choice_02", "Choice_03",
"Choice_04", "Choice_05", "Choice_06", "Choice_07", "Choice_08",
"Choice_09", "Choice_10", "Choice_11", "Choice_12"), control = c(18,
30, 47, 66, 86, 35, 31, 46, 55, 39, 55, 41), treatment = c(31,
35, 46, 68, 86, 36, 32, 42, 52, 39, 58, 43), control_p = c(0.163636363636364,
0.272727272727273, 0.427272727272727, 0.6, 0.781818181818182,
0.318181818181818, 0.281818181818182, 0.418181818181818, 0.5,
0.354545454545455, 0.5, 0.372727272727273), treatment_p = c(0.319587628865979,
0.360824742268041, 0.474226804123711, 0.701030927835051, 0.88659793814433,
0.371134020618557, 0.329896907216495, 0.43298969072165, 0.536082474226804,
0.402061855670103, 0.597938144329897, 0.443298969072165)), row.names = c(NA,
-12L), class = c("tbl_df", "tbl", "data.frame"))
# A tibble: 12 x 5
ECOST control treatment control_p treatment_p
<chr> <dbl> <dbl> <dbl> <dbl>
1 Choice_01 18 31 0.164 0.320
2 Choice_02 30 35 0.273 0.361
3 Choice_03 47 46 0.427 0.474
4 Choice_04 66 68 0.6 0.701
5 Choice_05 86 86 0.782 0.887
6 Choice_06 35 36 0.318 0.371
7 Choice_07 31 32 0.282 0.330
8 Choice_08 46 42 0.418 0.433
9 Choice_09 55 52 0.5 0.536
10 Choice_10 39 39 0.355 0.402
11 Choice_11 55 58 0.5 0.598
12 Choice_12 41 43 0.373 0.443
I want to melt this data, but I want the columns control
and control_p
to stay together, and the columns treatment
and treatment_p
to stay together, creating a table with 24 rows and 4 columns.
Desired result:
# A tibble: 12 x 5
ECOST count percentage group
<chr> <dbl> <dbl>
1 Choice_01 18 0.164 control
2 Choice_02 30 0.273 control
3 Choice_03 47 0.427 control
4 Choice_04 66 0.6 control
5 Choice_05 86 0.782 control
6 Choice_06 35 0.318 control
7 Choice_07 31 0.282 control
8 Choice_08 46 0.418 control
9 Choice_09 55 0.5 control
10 Choice_10 39 0.355 control
11 Choice_11 55 0.5 control
12 Choice_12 41 0.373 control
13 Choice_01 18 0.320 treatment
14 Choice_02 30 0.361 treatment
15 Choice_03 46 0.474 treatment
16 Choice_04 68 0.701 treatment
17 Choice_05 86 0.887 treatment
18 Choice_06 36 0.371 treatment
19 Choice_07 32 0.330 treatment
20 Choice_08 42 0.433 treatment
21 Choice_09 52 0.536 treatment
22 Choice_10 39 0.402 treatment
23 Choice_11 58 0.598 treatment
24 Choice_12 43 0.443 treatment