I am trying to align two sets of columns in order to compare them. Gathering the first set of columns followed by the second set leads to repetition.
Data:
data <- data.frame(
id=1:4,
q1= 1:4,
q1_1= 5:8,
q2= 9:12,
q2_1= 13:16
)
> data
id q1 q1_1 q2 q2_1
1 1 1 5 9 13
2 2 2 6 10 14
3 3 3 7 11 15
4 4 4 8 12 16
Desired result:
id key value key2 value2
1 1 q1 1 q2 9
2 2 q1 2 q2 10
3 3 q1 3 q2 11
4 4 q1 4 q2 12
5 1 q1_1 5 q2_1 13
6 2 q1_1 6 q2_1 14
7 3 q1_1 7 q2_1 15
8 4 q1_1 8 q2_1 16
Attempt:
data %>% gather(key, value, q1:q1_1) %>%
gather(key2, value2, q2, q2_1)