Row Labels Needed to Spread

No explanation for this set of notes, just a few reminders when spreading and gathering.

  b_partial b_wo_partial se_partial se_wo_partial
1         1            4          6             3
2         2            5          7             2
3         3            6          8             1

We want the columns to be “model,” “result,” and “value.”

Here is my incorrect attempt.

cd_try <- cd_try %>%
  gather(b_partial, b_wo_partial, key = 'model', value = 'b1') 

cd_try
  se_partial se_wo_partial        model b1
1          6             3    b_partial  1
2          7             2    b_partial  2
3          8             1    b_partial  3
4          6             3 b_wo_partial  4
5          7             2 b_wo_partial  5
6          8             1 b_wo_partial  6
cd_try <- cd_try %>%
  gather(se_partial, se_wo_partial, key = 'se_model', value = 'sd')

cd_try # not evaluated because it won't work

Instead, I need to gather everything in at the same time, split, and then spread.

  b_partial b_wo_partial se_partial se_wo_partial
1         1            4          6             3
2         2            5          7             2
3         3            6          8             1

Gather

cd_try <- cd_try %>%
  gather(b_partial, b_wo_partial, 
         se_partial, se_wo_partial,
         key = 'result_model', value = 'value') # gather everything

cd_try
    result_model value
1      b_partial     1
2      b_partial     2
3      b_partial     3
4   b_wo_partial     4
5   b_wo_partial     5
6   b_wo_partial     6
7     se_partial     6
8     se_partial     7
9     se_partial     8
10 se_wo_partial     3
11 se_wo_partial     2
12 se_wo_partial     1

Split

cd_try <- cd_try %>%
  separate(result_model, into = c('result', 'model'), sep = "_")

cd_try
   result   model value
1       b partial     1
2       b partial     2
3       b partial     3
4       b      wo     4
5       b      wo     5
6       b      wo     6
7      se partial     6
8      se partial     7
9      se partial     8
10     se      wo     3
11     se      wo     2
12     se      wo     1

Spread, BUT WHEN YOU SPREAD MAKE SURE TO INCLUDE ROW IDENTIFIERS.

cd_try <- cd_try %>%
  mutate(row_help = rep(1:6, 2))

cd_try <- cd_try %>%
  spread(result, value)

cd_try
    model row_help b se
1 partial        1 1  6
2 partial        2 2  7
3 partial        3 3  8
4      wo        4 4  3
5      wo        5 5  2
6      wo        6 6  1

Bo\(^2\)m =)