Hi people of stackoverflow,
I have trouble formatting my data frame efficiently. My original frame looks like this:
region transportation_type X2020.01.13 X2020.01.14 X2020.01.15 X2020.01.16 X2020.01.17
1 Akron driving 100.0 103.06 107.50 106.14 123.62
2 Akron transit 100.0 106.69 103.75 100.22 89.04
3 Akron walking 100.0 97.23 79.05 74.77 89.55
4 Albany driving 100.0 102.35 107.35 105.54 128.97
5 Albany transit 100.0 100.14 105.95 107.76 101.39
6 Albany walking 100.0 108.36 113.36 107.52 129.43
To merge it with some other data, I want to convert the transportation_type into columns (wide format) and the dates X2020.01.13-X2020.01.16 into one column (long format), like so:
region date driving transit walking
1 Akron X2020.01.13 100.0 100.0 100.0
2 Akron X2020.01.14 103.06 106.69 97.23
3 Akron X2020.01.15 107.50 103.75 79.05
4 Akron X2020.01.16 106.14 100.22 74.77
5 Akron X2020.01.17 123.62 89.04 89.55
6 Albany X2020.01.13 100.0 100.0 100.0
7 Albany X2020.01.14 103.06 106.69 97.23
8 Albany X2020.01.15 107.50 103.75 79.05
9 Albany X2020.01.16 106.14 100.22 74.77
10 Albany X2020.01.17 123.62 89.04 89.55
I can reformat using the in two steps, using e.g. the "melt" command, by first converting the transportation_type into wide format and then the dates into long.
Can I do it more efficiently in one step?
Thanks for your help!