0

Hey guys I am trying to build a pivot table in R which I just started using. I know this topic has been discussed a few times here but I can't find an example that would help me out. I have data table which looks like following enter image description here

What I am trying to do is build a triangle with Years as columns. Country & City as rows. So it looks like this

enter image description here

I have tried using group_by but can't figure it out. Would really appreciate any advice I can get!

Thanks in advance

  • 1
    Combination of `group_by()` and `pivot_wider()` would probably work. – Harrison Jones Jun 10 '22 at 17:59
  • `pivot_wider(df, c(Country, City), names_from = Year, values_from=Sales)` – Onyambu Jun 10 '22 at 18:05
  • Note that Excel's "summarize value field by" corresponds to the *values_fn* argument to `pivot_wider`. Example: `pivot_wider(..., values_fn = sum)`. –  Jun 10 '22 at 18:20

0 Answers0