# Tidy vs. untidy data

In R for Data Science, the authors explain their idea of tidy data. They give an example for data that is not tidy:

#> # A tibble: 3 x 3
#>   country         1999     2000
#> * <chr>            <int>      <int>
#> 1 Afghanistan   19987071   20595360
#> 2 Brazil       172006362  174504898
#> 3 China       1272915272 1280428583


A tidy version of the same data, according to the authors, would be:

#> # A tibble: 6 x 3
#>   country     year  population
#>   <chr>       <chr>      <int>
#> 1 Afghanistan 1999    19987071
#> 2 Afghanistan 2000    20595360
#> 3 Brazil      1999   172006362
#> 4 Brazil      2000   174504898
#> 5 China       1999  1272915272
#> 6 China       2000  1280428583


I see that in the untidy version, one must already know that the data is population, otherwise it is impossible to understand, what 1999 and 2000 mean. However, that could be derived from context, e.g. if the tibble is stored in a variable called population.

Now, who am I to doubt their judgment -- I do not. But I would like to better understand their idea. What are the actual advantages of the second version? I cannot not intuitively see them, e.g. calculation of mean etc. would be easy for both cases AFAICS, even if different functions need to be used. On the other hand, I would think that it is e.g. easier to calculate the correlation between the population of two years if the data is stored in the "untidy" form. What is my mistake here?

As you mention, the first example is data in an "untidy" format which can make analysis more difficult because of multiple reason. The first one being the column names, as you mention you do not know what the values in the columns actually represent. You mention that this can be derived from the context (e.g. the variable name), but what if the data is originally stored in a csv file called data.txt? In addition, how would this work if you have another type of value for which you also have data for 1999 and 2000, what would you name the columns? A second (and probably the biggest) advantage of using tidy data is that it allows for easier and more standardized analysis. I think the following from the original paper describes it well: