1
$\begingroup$

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?

$\endgroup$

1 Answer 1

1
$\begingroup$

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:

Tidy data makes it easy for an analyst or a computer to extract needed variables because it provides a standard way of structuring a dataset. Compare Table 3 to Table 1: in Table 1 you need to use different strategies to extract different variables. This slows analysis and invites errors. If you consider how many data analysis operations involve all of the values in a variable (every aggregation function), you can see how important it is to extract these values in a simple, standard way. Tidy data is particularly well suited for vectorised programming languages like R, because the layout ensures that values of different variables from the same observation are always paired.

Section 4 adn 5 from the paper give more in-depth information on how the tidy format works with existing functions within R and how to apply them.

$\endgroup$
1
  • $\begingroup$ Thanks very much. Could you maybe comment on the correlation between the population of two years? Intuitively, I'd say it is straightforward to calculate that in the "untidy" version as the data is "parallel". For the tidy version, I would first need to extract half of the rows. At least I do not see a direct way of doing it otherwise. $\endgroup$
    – DSnoob
    Commented Jan 13, 2022 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.