My issue is that data is stored in the column names. NAME_X_Y_OTHER. I need to combine multiple columns into one, but I need to keep the X and Y values.
The original data would look like this.
my_df <- data.frame(MISC=c("a","a","b"),
NAME_10_03_OTHER=c(1,2,3),
NAME_10_04_OTHER=c(4,5,6),
NAME_11_04_OTHER=c(7,8,9))
MISC NAME_10_03_OTHER NAME_10_04_OTHER NAME_11_04_OTHER
1 a 1 4 7
2 a 2 5 8
3 b 3 6 9
I would like to transform it to this.
my_new_df <- data.frame(MISC=c("a","a","b","a","a","b","a","a","b"),
NAME_OTHER=c(1,2,3,4,5,6,7,8,9),
x=c(10,10,10,10,10,10,11,11,11),
y=c(3,3,3,3,3,3,4,4,4))
MISC NAME_OTHER x y
1 a 1 10 3
2 a 2 10 3
3 b 3 10 3
4 a 4 10 3
5 a 5 10 3
6 b 6 10 3
7 a 7 11 4
8 a 8 11 4
9 b 9 11 4
I can combine the columns with c(t(my_df)), but I lose the X and Y values.