-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
106 lines (82 loc) · 3.29 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
---
output:
github_document:
df_print: kable
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
message = FALSE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# rsegregation
<!-- badges: start -->
[![Travis build status](https://travis-ci.org/arthurgailes/rsegregation.svg?branch=master)](https://travis-ci.org/arthurgailes/rsegregation)
[![Codecov test coverage](https://codecov.io/gh/arthurgailes/rsegregation/branch/master/graph/badge.svg)](https://codecov.io/gh/arthurgailes/rsegregation?branch=master)
[![R build status](https://github.com/arthurgailes/rsegregation/workflows/R-CMD-check/badge.svg)](https://github.com/arthurgailes/rsegregation/actions)
<!-- badges: end -->
A shortcut package with formulas for several different indices of segregation. rsegregation is designed to fit into the tidyverse framework, particularly dplyr.
## Installation
<!--
You can install the released version of rsegregation from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("rsegregation")
```
-->
The development version from [GitHub](https://github.com/) can be installed with:
``` {r eval=FALSE}
# install.packages("devtools")
devtools::install_github("arthurgailes/rsegregation")
```
## Usage
rsegregation depends upon dplyr (>1.0.0), and can be used with it. To return a single divergence score for Bay Area County:
### Divergence and Entropy
#### Calculate the divergence score for the entire dataset
rsegregation can work with base r, or within several dplyr verbs:
```{r bay_divergence, results='hide'}
library(rsegregation)
library(dplyr)
## included dataset of Bay Area Census tracts
# Using dplyr
bay_divergence <- bay_race %>%
summarize(bay_divergence = divergence(white,black,asian, hispanic, all_other,
population=total_pop, summed = T))
# Using base r
bay_divergence <- divergence(bay_race[c('white','black','asian', 'hispanic', 'all_other')],
population=bay_race$total_pop, summed = T)
# or
bay_divergence <- divergence(bay_race$white,bay_race$black,bay_race$asian,
bay_race$hispanic, bay_race$all_other, population=bay_race$total_pop, summed = T)
# all return the same result:
bay_divergence
```
#### Calculate divergence by group
Using the included Bay Area dataset of 2010 racial groups, divergence can be calculated by county using `dplyr::group_by()`.
```{r by_county}
#library(dplyr)
group_by(bay_race, county) %>%
summarize(bay_divergence = divergence(white,black,asian, hispanic, all_other,
population=total_pop, summed = T))
```
#### By-observation divergence scores
Divergence and entropy are both calculated rowwise by default (summed = FALSE).
```{r by_tract}
bay_entropy <- bay_race
bay_entropy$entropy <- entropy(bay_race[c('white','black','asian',
'hispanic','all_other')], population=bay_race$total_pop, summed = F)
head(bay_entropy)
```
### Miscellaneous
Dataframes should be formatted as long on geographic observations (e.g. tracts), but wide on group observations (e.g. races), as in the included dataset of the San Francisco Bay Area.
```{r bay}
head(bay_race)
```
## Future development:
* decomposition of entropy index
* more measures of segregation
## License
This package is free and open source software, licensed under GPL-3.