I have a dataframe that looks like this
+---------+--------+--------+--------+-------+
| ID | week1_t| week1_a| week2_t|week2_a|
+---------+--------+--------+--------+-------+
| 1 | 12 | 22 | 17 | 4 |
| 1 | 15 | 32 | 18 | 5 |
| 1 | 24 | 12 | 29 | 6 |
| 2 | 45 | 11 | 19 | 8 |
| 2 | 23 | 33 | 20 | 10 |
+---------+--------+--------+--------+-------+
There are 48 columns (week 1 - 24) with "t" and "a" suffixes. I want to consolidate all the week columns into a single "week" column like this:
+---------+--------+--------+--------
| ID | week | t | a |
+---------+--------+--------+--------
| 1 | 1 | 22 | 17 |
| 1 | 2 | 32 | 18 |
| 1 | 3 | 12 | 19 |
| 1 | 5 | 33 | 20 |
+---------+--------+--------+-------
How do I go about making this transformation in R? I can't think of a way of doing this outside multiple if statements and for loops.
Data
dd <- read.table(header = TRUE, text = "ID week1_t week1_a week2_t week2_a
1 12 22 17 4
1 15 32 18 5
1 24 12 29 6
2 45 11 19 8
2 23 33 20 10")