obs pre.data1 post.data1 pre.data2 post.data2 pre.data3 post.data3
1 0.40 0.12 0.61 0.15 0.58 0.06
2 0.21 0.05 0.18 0.49 0.35 0.24
3 0.48 0.85 0.00 0.62 0.96 0.37
4 0.66 0.29 0.88 0.56 0.13 0.72
5 0.43 0.23 0.80 0.78 0.05 0.90
6 0.86 0.04 0.25 0.34 0.99 0.79
7 0.57 0.86 0.20 0.34 0.11 0.34
8 0.13 0.39 0.24 0.51 0.79 0.63
9 0.87 0.57 0.00 0.55 0.86 0.72
I've attached an example of my dataset above.
I'm trying to convert from wide to long format with tidyr based off of the prefixes pre and post of each variable. Ideally I would like my data to be in the following format
obs key data1 data2 data3
1 pre 0.50 0.26 0.12
1 post 0.18 0.50 0.42
2 pre 0.06 0.07 0.47
2 post 0.98 0.87 0.89
3 pre 0.34 0.55 0.72
3 post 0.26 0.99 0.71
4 pre 0.64 0.80 0.54
4 post 0.01 0.36 0.38
I'm fairly new to R but I know it uses the gather function, but I'm only familiar with gather with 2 columns as shown on many tutorial. Is there a simple way of doing the above? Thank you