forked from juliasilge/widyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
91 lines (61 loc) · 3.54 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
### widyr: Widen, process, and re-tidy a dataset
**License:** [MIT](https://opensource.org/licenses/MIT)
[![Travis-CI Build Status](https://travis-ci.org/dgrtwo/widyr.svg?branch=master)](https://travis-ci.org/dgrtwo/widyr)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/dgrtwo/widyr?branch=master&svg=true)](https://ci.appveyor.com/project/dgrtwo/widyr)
[![Coverage Status](https://img.shields.io/codecov/c/github/dgrtwo/widyr/master.svg)](https://codecov.io/github/dgrtwo/widyr?branch=master)
This package wraps the pattern of un-tidying data into a wide matrix, performing some processing, then turning it back into a tidy form. This is useful for several mathematical operations such as co-occurence counts, correlations, or clustering that are best done on a wide matrix.
### Installation
Install from Github with [devtools](https://github.com/hadley/devtools):
```{r, eval = FALSE}
library(devtools)
install_github("dgrtwo/widyr")
```
## Towards a precise definition of "wide" data
The term "wide data" has gone out of fashion as being "imprecise" [(Wickham 2014)](http://vita.had.co.nz/papers/tidy-data.pdf)). I think with a proper definition the term could be entirely meaningful and useful.
A **wide** dataset is one or more matrices where:
* Each row is one **item**
* Each column is one **feature**
* Each value is one **observation**
* Each matrix is one **variable**
When would you want data to be wide rather than tidy? Notable examples include classification, clustering, factorization, or other operations that can take advantage of a matrix structure. In general, when you want to **compare across items** rather than compare between variables, this is a useful structure.
### Example: gapminder
Consider the gapminder dataset in the [gapminder package](https://cran.r-project.org/web/packages/gapminder/index.html).
```{r}
library(dplyr)
library(gapminder)
gapminder
```
This tidy format (one-row-per-country-per-year) is very useful for grouping, summarizing, and filtering operations. But if we want to *compare* countries (for example, to find countries that are similar to each other), we would have to reshape this dataset. Note that here, country is the **item**, while year is the **feature** column.
#### Pairwise operations
The widyr package offers `pairwise_` functions that operate on pairs of items. An example is `pairwise_dist`:
```{r}
library(widyr)
gapminder %>%
pairwise_dist(country, year, lifeExp)
```
In a single step, this finds the Euclidean distance between the `lifeExp` value in each pair of countries, matching by year. We could find the closest pairs of countries overall using the `sort = TRUE` argument:
```{r}
gapminder %>%
pairwise_dist(country, year, lifeExp, sort = TRUE)
```
Notice that this includes duplicates (Germany/Belgium and Belgium/Germany). To avoid those (the upper triangle of the distance matrix), use `upper = FALSE`:
```{r}
gapminder %>%
pairwise_dist(country, year, lifeExp, upper = FALSE) %>%
arrange(distance)
```
In some analyses, we may be interested in correlation rather than distance of pairs. For this we would use `pairwise_cor`:
```{r}
gapminder %>%
pairwise_cor(country, year, lifeExp, upper = FALSE, sort = TRUE)
```
### Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.