0

I have a dataset for country data that has a bunch of information listed in a long list, but I want to rearrange the data so it works well for a time series chart width-wise.

An example of what I have now:

Value   Year    Indicator   Country
500     2015    GDP         Germany
450     2014    GDP         Germany
400     2013    GDP         Germany
350     2012    GDP         Germany
300     2011    GDP         Germany
800     2015    GDP         China
750     2014    GDP         China
735     2013    GDP         China
700     2012    GDP         China
360     2011    GDP         China
900     2015    GDP         Italy
670     2014    GDP         Italy
550     2013    GDP         Italy
430     2012    GDP         Italy
200     2011    GDP         Italy

And how I'd like it to look:

Year    Germany China   Italy
2015    500     800     900
2014    450     750     670
2013    400     735     550
2012    350     700     430
2011    300     360     200

Are there any functions I should be considering to do this? I've looked into melt() and reshape(), but I'm a little unclear with how to proceed with them given the layout of the data.

I really appreciate anyone's help. Thanks in advance.

Natasha R.
  • 521
  • 1
  • 5
  • 11
  • 1
    Can you please do dput of your data – Hardik Gupta Apr 29 '17 at 04:20
  • In R this is called reshape, or melt, or cast. Your data is 'long-form' and you want to reshape it to 'wide-form'. There are a ton of existing duplicates, although they haven't had the tidyverse syntax added yet. See e.g. [How to reshape data from long to wide format?](http://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format) or [Reshape three column data frame to matrix (“long” to “wide” format)](http://stackoverflow.com/questions/9617348/reshape-three-column-data-frame-to-matrix-long-to-wide-format) – smci Apr 29 '17 at 04:30
  • dcast(data[,!c("Year", "Country", "Value")], Year ~ Country) – Edgar Santos Apr 29 '17 at 04:47

1 Answers1

0

You can use the spread function from the tidyverse package in R.Lets say your datasetname is country_gdp

country_gdp[,-3] %>% spread(Country,Value)