Skip to content

Instantly share code, notes, and snippets.

@kcsluis
Created September 12, 2015 00:21
Show Gist options
  • Save kcsluis/65a26c40389ce0929023 to your computer and use it in GitHub Desktop.
Save kcsluis/65a26c40389ce0929023 to your computer and use it in GitHub Desktop.
Data Manipulation
yield variety year site
27 Manchuria 1931 University Farm
48.86667 Manchuria 1931 Waseca
27.43334 Manchuria 1931 Morris
39.93333 Manchuria 1931 Crookston
32.96667 Manchuria 1931 Grand Rapids
28.96667 Manchuria 1931 Duluth
43.06666 Glabron 1931 University Farm
55.2 Glabron 1931 Waseca
28.76667 Glabron 1931 Morris
38.13333 Glabron 1931 Crookston
29.13333 Glabron 1931 Grand Rapids
29.66667 Glabron 1931 Duluth
35.13333 Svansota 1931 University Farm
47.33333 Svansota 1931 Waseca
25.76667 Svansota 1931 Morris
40.46667 Svansota 1931 Crookston
29.66667 Svansota 1931 Grand Rapids
25.7 Svansota 1931 Duluth
39.9 Velvet 1931 University Farm
50.23333 Velvet 1931 Waseca
26.13333 Velvet 1931 Morris
41.33333 Velvet 1931 Crookston
23.03333 Velvet 1931 Grand Rapids
26.3 Velvet 1931 Duluth
36.56666 Trebi 1931 University Farm
63.8333 Trebi 1931 Waseca
43.76667 Trebi 1931 Morris
46.93333 Trebi 1931 Crookston
29.76667 Trebi 1931 Grand Rapids
33.93333 Trebi 1931 Duluth
43.26667 No. 457 1931 University Farm
58.1 No. 457 1931 Waseca
28.7 No. 457 1931 Morris
45.66667 No. 457 1931 Crookston
32.16667 No. 457 1931 Grand Rapids
33.6 No. 457 1931 Duluth
36.6 No. 462 1931 University Farm
65.7667 No. 462 1931 Waseca
30.36667 No. 462 1931 Morris
48.56666 No. 462 1931 Crookston
24.93334 No. 462 1931 Grand Rapids
28.1 No. 462 1931 Duluth
32.76667 Peatland 1931 University Farm
48.56666 Peatland 1931 Waseca
29.86667 Peatland 1931 Morris
41.6 Peatland 1931 Crookston
34.7 Peatland 1931 Grand Rapids
32 Peatland 1931 Duluth
24.66667 No. 475 1931 University Farm
46.76667 No. 475 1931 Waseca
22.6 No. 475 1931 Morris
44.1 No. 475 1931 Crookston
19.7 No. 475 1931 Grand Rapids
33.06666 No. 475 1931 Duluth
39.3 Wisconsin No. 38 1931 University Farm
58.8 Wisconsin No. 38 1931 Waseca
29.46667 Wisconsin No. 38 1931 Morris
49.86667 Wisconsin No. 38 1931 Crookston
34.46667 Wisconsin No. 38 1931 Grand Rapids
31.6 Wisconsin No. 38 1931 Duluth
26.9 Manchuria 1932 University Farm
33.46667 Manchuria 1932 Waseca
34.36666 Manchuria 1932 Morris
32.96667 Manchuria 1932 Crookston
22.13333 Manchuria 1932 Grand Rapids
22.56667 Manchuria 1932 Duluth
36.8 Glabron 1932 University Farm
37.73333 Glabron 1932 Waseca
35.13333 Glabron 1932 Morris
26.16667 Glabron 1932 Crookston
14.43333 Glabron 1932 Grand Rapids
25.86667 Glabron 1932 Duluth
27.43334 Svansota 1932 University Farm
38.5 Svansota 1932 Waseca
35.03333 Svansota 1932 Morris
20.63333 Svansota 1932 Crookston
16.63333 Svansota 1932 Grand Rapids
22.23333 Svansota 1932 Duluth
26.8 Velvet 1932 University Farm
37.4 Velvet 1932 Waseca
38.83333 Velvet 1932 Morris
32.06666 Velvet 1932 Crookston
32.23333 Velvet 1932 Grand Rapids
22.46667 Velvet 1932 Duluth
29.06667 Trebi 1932 University Farm
49.2333 Trebi 1932 Waseca
46.63333 Trebi 1932 Morris
41.83333 Trebi 1932 Crookston
20.63333 Trebi 1932 Grand Rapids
30.6 Trebi 1932 Duluth
26.43334 No. 457 1932 University Farm
42.2 No. 457 1932 Waseca
43.53334 No. 457 1932 Morris
34.33333 No. 457 1932 Crookston
19.46667 No. 457 1932 Grand Rapids
22.7 No. 457 1932 Duluth
25.56667 No. 462 1932 University Farm
44.7 No. 462 1932 Waseca
47 No. 462 1932 Morris
30.53333 No. 462 1932 Crookston
19.9 No. 462 1932 Grand Rapids
22.5 No. 462 1932 Duluth
28.06667 Peatland 1932 University Farm
36.03333 Peatland 1932 Waseca
43.2 Peatland 1932 Morris
25.23333 Peatland 1932 Crookston
26.76667 Peatland 1932 Grand Rapids
31.36667 Peatland 1932 Duluth
30 No. 475 1932 University Farm
41.26667 No. 475 1932 Waseca
44.23333 No. 475 1932 Morris
32.13333 No. 475 1932 Crookston
15.23333 No. 475 1932 Grand Rapids
27.36667 No. 475 1932 Duluth
38 Wisconsin No. 38 1932 University Farm
58.16667 Wisconsin No. 38 1932 Waseca
47.16667 Wisconsin No. 38 1932 Morris
35.9 Wisconsin No. 38 1932 Crookston
20.66667 Wisconsin No. 38 1932 Grand Rapids
29.33333 Wisconsin No. 38 1932 Duluth
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
</style>
</head>
<body>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
<script>
d3.tsv("barley.tsv", ready);
function ready(error, data) {
if (error) return console.warn(error);
// declare a function
function functionName(functionVariables) {
// foo
};
// anonymous functions are used often
var anon = function (d) { return d.foo; };
// these functions can also return an index
var anonIndex = function (d, i) { return d.foo+i; };
// use .map to extract from a larger data set
var yields = data.map(function (d) { return d.yield; });
// use .filter to select data conditionally
var grandRapidsData = data.filter(function (d) { return d.site == 'Grand Rapids'; });
// use .reduce to sum - (a, b) is critical
var totalYields = yields.reduce( function (a,b) {
return a + b;
});
// use .sort to order data = (a,b) is critical
data.sort(function (a,b) { return a.yield - b.yield; });
// map and sort together to dedupe
var allVarieties = data.map( function (d) { return d.variety; });
var allVarietiesDeduped = d3.set(allVarieties).values();
// min, max, extent (extent returns array with two values)
var yieldsMin = d3.min(yields);
var yieldsMax = d3.max(yields);
var yieldsExtent = (d3.extent(data, function (d) { return d.yield; }));
// rounding
var rounded = Math.round(23.2);
var roundedDecimals = (23.42422).toFixed(2);
// reduce w. a ternary operator
var sumYieldsOver50 = yields.reduce(function (a, b) {
return (a > 50) ? a + b : b;
});
data.forEach( function(d) {
// .split, returns array of characters split by character
d.split = d.yield.split(".");
// .replace
d.replace = d.yield.replace(".", ",");
// trim whitespace
d.trim = d.site.replace(/\d/g,'').trim();
// substring selection
var str = d.site;
var strSubstring = str.substring(0, str.length);
});
// date
var s = [0,2];
var date = new Date(s[0],(s[1]-1));
// month index is 0 based (jan is 0, feb is 1)
// day is 1 based
// will add commas
function formatNumber (num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
};
function addCommas(nStr) {
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
};
// for x in y (x can be whatever you like)
for (yield in yields) {
// console.log(yield);
};
// other for loop
allVarietiesDeduped.forEach(function (d) {
// console.log(d);
});
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment