Skip to content

Instantly share code, notes, and snippets.

@curran
Last active December 3, 2022 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curran/851f506aac452bf962d3 to your computer and use it in GitHub Desktop.
Save curran/851f506aac452bf962d3 to your computer and use it in GitHub Desktop.
Crime by Race Dataset (Normalized)

This is a dataset on crime in 2014, subdivided by race and offense. Originally found in table 43 in the FBI Web Site "2014 Crime in the United States". This is the same data as in a previous block, but normalized such that "Race" is now a single column rather than a collection of different columns. This allows the data to be easily used with d3.nest and other utilities/

This example page provides the basic code required to load the data and display it on the page (as JSON) using D3.js.

Built with blockbuilder.org

forked from curran's block: Crime by Race Dataset

web counter
Offense charged Total White Black or African American American Indian or Alaska Native Asian Native Hawaiian or Other Pacific Islander
TOTAL 8730665 6056687 2427683 135599 100067 10629
Murder and nonnegligent manslaughter 8230 3807 4224 83 107 9
Rape 16326 10977 4888 212 222 27
Robbery 74077 31354 41379 616 617 111
Aggravated assault 291600 185612 96511 4372 4507 598
Burglary 186794 126242 56504 1703 1999 346
Larceny-theft 971199 671260 271788 15869 11355 927
Motor vehicle theft 53456 35551 16391 668 677 169
Arson 7298 5338 1709 142 98 11
Violent crime 390233 231750 147002 5283 5453 745
Property crime 1218747 838391 346392 18382 14129 1453
Other assaults 853887 558181 272068 13618 9173 847
Forgery and counterfeiting 44336 28272 15095 279 646 44
Fraud 109576 72424 34853 1116 1124 59
Embezzlement 12678 7851 4518 87 210 12
Stolen property; buying, receiving, possessing 69912 45816 22538 662 812 84
Vandalism 154755 108531 41723 2761 1556 184
Weapons; carrying, possessing, etc. 109891 62920 44705 803 1307 156
Prostitution and commercialized vice 37030 19867 15483 199 1434 47
Sex offenses (except rape and prostitution) 43125 31279 10462 640 688 56
Drug abuse violations 1216225 837851 353862 10071 12893 1548
Gambling 4363 1560 2568 16 192 27
Offenses against the family and children 79075 50912 26048 1521 574 20
Driving under the influence 863598 722451 112107 12048 15938 1054
Liquor laws 246304 197559 35727 9539 3302 177
Drunkenness 327325 264906 51485 6952 3552 430
Disorderly conduct 338636 213342 114802 7734 2493 265
Vagrancy 21577 14819 6116 443 182 17
All other offenses (except traffic) 2546822 1726091 750488 42980 23946 3317
Suspicion 1057 557 472 15 13 0
Curfew and loitering law violations 41513 21357 19169 450 450 87
{
"columns":[
{ "name": "Offense charged", "type": "string" },
{ "name": "Total", "type": "number" },
{ "name": "White", "type": "number" },
{ "name": "Black or African American", "type": "number" },
{ "name": "American Indian or Alaska Native", "type": "number" },
{ "name": "Asian", "type": "number" },
{ "name": "Native Hawaiian or Other Pacific Islander", "type": "number" }
]
}
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.9/d3.min.js"></script>
<script src="//curran.github.io/dsv-dataset/dsv-dataset-v0.2.1.js"></script>
</head>
<body>
<script>
function render(data){
// This computes totals for each offense.
// Could be a bar chart.
var offenses = d3.nest()
.key(function (d){ return d["Offense charged"];})
.rollup(function(leaves) {
return d3.sum(leaves, function(d) { return d.count; });
})
.entries(data);
d3.select("body")
.append("pre")
.text(JSON.stringify(offenses));
// This computes totals for each race.
// Could be a bar chart.
var offenses = d3.nest()
.key(function (d){ return d["Race"];})
.rollup(function(leaves) {
return d3.sum(leaves, function(d) { return d.count; });
})
.entries(data);
d3.select("body")
.append("pre")
.text(JSON.stringify(offenses));
// This shows the full data.
// Could be a stacked or grouped bar chart.
d3.select("body")
.append("pre")
.text(JSON.stringify(data, null, 2));
}
d3.csv("data.csv", function(data){
d3.json("data.json", function(metadata){
var dataset = dsvDataset.parse({
data: data,
metadata: metadata
});
// Transform the table so "Race" is a single column.
data = normalize(dataset.data, "Race", "count", [
"White",
"Black or African American",
"American Indian or Alaska Native",
"Asian",
"Native Hawaiian or Other Pacific Islander"
]);
// Remove the "TOTAL" values.
data = data.filter(function (d){
return d["Offense charged"] !== "TOTAL";
});
data.forEach(function (d){
delete d["Total"];
});
render(data);
});
});
function normalize(dataIn, dimension, measure, values){
var data = [];
// All the columns not in "values".
var columns = Object.keys(dataIn[0]).filter(function (column){
return !values.some(function (value){
return column === value;
});
});
dataIn.forEach(function (d){
values.forEach(function (value){
var row = {};
// Pivot the given value columns into rows.
row[dimension] = value;
row[measure] = d[value];
// Copy over values from other columns.
columns.forEach(function (column){
row[column] = d[column];
});
data.push(row);
});
});
return data;
}
</script>
</body>
@bogdanblazhkevych
Copy link

despite...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment