0

I have a data frame that looks like this:

|                | 2005    | 2005    | 2005   | 2005   | 2006   | ...
|                | Q1      | Q2      |  Q3    |  Q4    |  Q1    |
| Goods          | 85.829  | 86.371  | 87.304 | 87.999 | 88.456 |
| Durable Goods  | 90.065  | 90.393  | 91.893 | 91.986 | 91.909 | 
| Motor Vehicles | 94.145  | 94.514  | 93.809 | 94.408 | 94.630 |
...

And I need it to look like this:

**| Item  | Year | Quarter | Value  |**
  | Goods | 2005 | Q1      | 85.829 |
  | Goods | 2005 | Q2      | 86.371 |
  | Goods | 2005 | Q3      | 87.304 |
...
 | Durable Goods | 2005 | Q1 | 90.065 |
 | Durable Goods | 2005 | Q2 | 90.393 |
 | Durable Goods | 2005 | Q3 | 91.893 |
...

The year range is from 2005 to 2017, 4 quarters for every year.

I have tried using melt() and reshape() as well as transposing the data and working with it from that format, but I keep getting messy results.

For reference, I am working in R studio. I prefer to use tidyverse for my data, but I am willing to use any packages necessary for a solution.

Bas H
  • 2,114
  • 10
  • 14
  • 23
  • Reshapeing from wide-to-long is a very common question. The duplicate has many alternatives. The most common is `pivot_longer`. But it's not clear exactly how your data is structured. It's better to share data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) like `dput()` so we can see exactly how your data has been read into R. R data.frames don't support nested header columns so it's not clear how you can have that. If they are joined in one header, then you can split those values after reshaping. – MrFlick Apr 27 '23 at 14:59

0 Answers0