I have the following tibble:
~id, ~a, ~b, ~c, ~d,
1, 10, 20, 33, 42,
2, 30, 20, 32, 42,
3, 34, 24, 35, 32,
4, 32, 24, 35, 25,
5, 22, 14, 35, 36,
...
)
Use ?gather
to read more on how to use the gather
function.
library(dplyr)
library(tidyr)
oldTable %>% select(-c, -d) %>% gather(key = aOrB, value = value, -id)
EDIT: If you want to keep c
and d
, but not stacked:
oldTable %>% gather(key = aOrB, value = value, -id, -c, -d)
If you want to keep c
and d
, and stacked along with a
and b
:
oldTable %>% gather(key = aOrBOrCOrd, value = value, -id)