0

I'm not even sure how to describe what I want to achieve so I apologize for the unhelpful title.

Say I have a data frame with 3 columns: countries, year and population (ignore the population numbers, I just made them up)

Countries |   Year  |  Population
          |         |
Japan     |   1950  |  50
Japan     |   1990  |  70
Australia |   1950  |  25
Australia |   1990  |  50
Russia    |   1950  |  75
Russia    |   1990  |  100
China     |   1950  |  125
China     |   1990  |  150

I know that there are only two unique years (1950 and 1990) for every country. I want to turn it into this table

              Population
Country  |  1950   |  1990
---------|-----------------------    
Japan    |  50     |  70
Australia|  25     |  50
Russia   |  75     |  100
China    |  125    |  150

How do I do this?

  • `tidyr::pivot_wider(names_from = Year, values_from = Population)` should do it. If you have trouble, work through some of the examples at the linked FAQ or at the bottom of the `?pivot_wider` help page. – Gregor Thomas Oct 08 '20 at 17:55
  • `df %>%pivot_wider(names_from = Year,values_from=Population)` – Duck Oct 08 '20 at 17:56

0 Answers0