Skip to content

Instantly share code, notes, and snippets.

@SpaceActuary
Created December 4, 2015 21:46
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 SpaceActuary/723b26e187e6bbc2608f to your computer and use it in GitHub Desktop.
Save SpaceActuary/723b26e187e6bbc2608f to your computer and use it in GitHub Desktop.
un-nest
fips totalpop pctHispanic year
6001 1510271 0.225 2010
6003 1175 0.071 2010
6005 38091 0.125 2010
6007 220000 0.141 2010
6009 45578 0.103 2010
6011 21419 0.551 2010
6013 1049025 0.244 2010
6015 28610 0.178 2010
6017 181058 0.121 2010
6019 930450 0.503 2010
6021 28122 0.375 2010
6023 134623 0.098 2010
6025 174528 0.804 2010
6027 18546 0.194 2010
6029 839631 0.492 2010
6031 152982 0.509 2010
6033 64665 0.171 2010
6035 34895 0.175 2010
6037 9818605 0.477 2010
6039 150865 0.537 2010
6041 252409 0.155 2010
6043 18251 0.092 2010
6045 87841 0.222 2010
6047 255793 0.549 2010
6049 9686 0.139 2010
6051 14202 0.265 2010
6053 415057 0.554 2010
6055 136484 0.322 2010
6057 98764 0.085 2010
6059 3010232 0.337 2010
6061 348432 0.128 2010
6063 20007 0.080 2010
6065 2189641 0.455 2010
6067 1418788 0.216 2010
6069 55269 0.564 2010
6071 2035210 0.492 2010
6073 3095313 0.320 2010
6075 805235 0.151 2010
6077 685306 0.389 2010
6079 269637 0.208 2010
6081 718451 0.254 2010
6083 423895 0.429 2010
6085 1781642 0.269 2010
6087 262382 0.320 2010
6089 177223 0.084 2010
6091 3240 0.083 2010
6093 44900 0.103 2010
6095 413344 0.240 2010
6097 483878 0.249 2010
6099 514453 0.419 2010
6101 94737 0.288 2010
6103 63463 0.219 2010
6105 13786 0.070 2010
6107 442179 0.606 2010
6109 55365 0.107 2010
6111 823318 0.403 2010
6113 200849 0.303 2010
6115 72155 0.250 2010
6001 1443741 0.190 2000
6003 1208 0.078 2000
6005 35100 0.089 2000
6007 203171 0.105 2000
6009 40554 0.068 2000
6011 18804 0.465 2000
6013 948816 0.177 2000
6015 27507 0.139 2000
6017 156299 0.093 2000
6019 799407 0.440 2000
6021 26453 0.296 2000
6023 126518 0.065 2000
6025 142361 0.722 2000
6027 17945 0.126 2000
6029 661645 0.384 2000
6031 129461 0.436 2000
6033 58309 0.114 2000
6035 33828 0.138 2000
6037 9519338 0.446 2000
6039 123109 0.443 2000
6041 247289 0.111 2000
6043 17130 0.078 2000
6045 86265 0.165 2000
6047 210554 0.453 2000
6049 9449 0.115 2000
6051 12853 0.177 2000
6053 401762 0.468 2000
6055 124279 0.237 2000
6057 92033 0.057 2000
6059 2846289 0.308 2000
6061 248399 0.097 2000
6063 20824 0.057 2000
6065 1545387 0.362 2000
6067 1223499 0.160 2000
6069 53234 0.479 2000
6071 1709434 0.392 2000
6073 2813833 0.267 2000
6075 776733 0.141 2000
6077 563598 0.305 2000
6079 246681 0.163 2000
6081 707161 0.219 2000
6083 399347 0.342 2000
6085 1682585 0.240 2000
6087 255602 0.268 2000
6089 163256 0.055 2000
6091 3555 0.060 2000
6093 44301 0.076 2000
6095 394542 0.176 2000
6097 458614 0.173 2000
6099 446997 0.317 2000
6101 78930 0.222 2000
6103 56039 0.158 2000
6105 13022 0.040 2000
6107 368021 0.508 2000
6109 54501 0.082 2000
6111 753197 0.334 2000
6113 168660 0.259 2000
6115 60219 0.174 2000
6001 1279182 0.142 1990
6003 1113 0.066 1990
6005 30039 0.084 1990
6007 182120 0.075 1990
6009 31998 0.054 1990
6011 16275 0.333 1990
6013 803732 0.114 1990
6015 23460 0.103 1990
6017 125995 0.070 1990
6019 667490 0.355 1990
6021 24798 0.200 1990
6023 119118 0.042 1990
6025 109303 0.658 1990
6027 18281 0.084 1990
6029 543477 0.280 1990
6031 101469 0.341 1990
6033 50631 0.072 1990
6035 27598 0.104 1990
6037 8863164 0.378 1990
6039 88090 0.345 1990
6041 230096 0.078 1990
6043 14302 0.049 1990
6045 80345 0.103 1990
6047 178403 0.326 1990
6049 9678 0.072 1990
6051 9956 0.113 1990
6053 355660 0.336 1990
6055 110765 0.144 1990
6057 78510 0.042 1990
6059 2410556 0.234 1990
6061 172796 0.080 1990
6063 19739 0.046 1990
6065 1170413 0.263 1990
6067 1041219 0.117 1990
6069 36697 0.458 1990
6071 1418380 0.267 1990
6073 2498016 0.204 1990
6075 723959 0.139 1990
6077 480628 0.234 1990
6079 217162 0.133 1990
6081 649623 0.176 1990
6083 369608 0.266 1990
6085 1497577 0.210 1990
6087 229734 0.204 1990
6089 147036 0.038 1990
6091 3318 0.055 1990
6093 43531 0.059 1990
6095 340421 0.134 1990
6097 388222 0.106 1990
6099 370522 0.218 1990
6101 64415 0.164 1990
6103 49625 0.103 1990
6105 13063 0.033 1990
6107 311921 0.388 1990
6109 48456 0.077 1990
6111 669016 0.264 1990
6113 141092 0.200 1990
6115 58228 0.116 1990
6001 1105379 0.118 1980
6003 1097 0.038 1980
6005 19314 0.047 1980
6007 143851 0.052 1980
6009 20710 0.044 1980
6011 12791 0.195 1980
6013 656380 0.085 1980
6015 18217 0.056 1980
6017 85812 0.046 1980
6019 514621 0.293 1980
6021 21350 0.119 1980
6023 108514 0.034 1980
6025 92110 0.558 1980
6027 17895 0.061 1980
6029 403089 0.216 1980
6031 73738 0.269 1980
6033 36366 0.052 1980
6035 21661 0.065 1980
6037 7477503 0.276 1980
6039 63116 0.268 1980
6041 222568 0.041 1980
6043 11108 0.045 1980
6045 66738 0.055 1980
6047 134560 0.253 1980
6049 8610 0.041 1980
6051 8577 0.047 1980
6053 290444 0.259 1980
6055 99199 0.087 1980
6057 51645 0.030 1980
6059 1932709 0.148 1980
6061 117247 0.070 1980
6063 17340 0.037 1980
6065 663166 0.188 1980
6067 783381 0.095 1980
6069 25005 0.457 1980
6071 895016 0.185 1980
6073 1861846 0.148 1980
6075 678974 0.123 1980
6077 347342 0.192 1980
6079 155435 0.095 1980
6081 587329 0.125 1980
6083 298694 0.185 1980
6085 1295071 0.175 1980
6087 188141 0.147 1980
6089 115715 0.030 1980
6091 3073 0.064 1980
6093 39732 0.048 1980
6095 235203 0.105 1980
6097 299681 0.069 1980
6099 265900 0.150 1980
6101 52246 0.117 1980
6103 38888 0.055 1980
6105 11858 0.027 1980
6107 245738 0.298 1980
6109 33928 0.052 1980
6111 529174 0.214 1980
6113 113374 0.171 1980
6115 49733 0.088 1980
6001 1073180 0.126 1970
6003 510 NA 1970
6005 11795 0.076 1970
6007 101969 0.051 1970
6009 13585 0.057 1970
6011 12430 0.172 1970
6013 558389 0.093 1970
6015 14580 0.046 1970
6017 43833 0.044 1970
6019 413053 0.252 1970
6021 17521 0.106 1970
6023 99692 0.040 1970
6025 74492 0.460 1970
6027 15571 0.073 1970
6029 329162 0.168 1970
6031 66019 0.247 1970
6033 19548 0.045 1970
6035 16796 0.078 1970
6037 7036457 0.183 1970
6039 41519 0.240 1970
6041 206038 0.058 1970
6043 6015 0.026 1970
6045 51101 0.050 1970
6047 104629 0.229 1970
6049 7469 0.078 1970
6051 4016 0.012 1970
6053 250071 0.211 1970
6055 79140 0.075 1970
6057 26346 0.046 1970
6059 1420386 0.113 1970
6061 77306 0.093 1970
6063 11707 0.038 1970
6065 459072 0.173 1970
6067 631498 0.092 1970
6069 18226 0.449 1970
6071 681092 0.160 1970
6073 1357387 0.128 1970
6075 715673 0.142 1970
6077 290207 0.180 1970
6079 105690 0.111 1970
6081 556234 0.113 1970
6083 264324 0.173 1970
6085 1064714 0.175 1970
6087 123788 0.118 1970
6089 77640 0.038 1970
6091 2359 0.144 1970
6093 33231 0.054 1970
6095 169941 0.108 1970
6097 204885 0.074 1970
6099 194506 0.126 1970
6101 41935 0.092 1970
6103 29517 0.058 1970
6105 7615 0.022 1970
6107 188322 0.262 1970
6109 22169 0.070 1970
6111 376430 0.196 1970
6113 91788 0.167 1970
6115 44736 0.074 1970
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
svg { width: 100%; height: 100%; }
</style>
</head>
<body>
<script>
var margin = {top: 20, right: 10, bottom: 20, left: 10};
var width = 960 - margin.left - margin.right;
var height = 500 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.tsv("data.tsv", function(data){
var nested = d3.nest()
.key(function(d) { return d.year; })
.entries(data);
console.log("nested", nested);
var unnested = function(data, children){
var out = [];
data.forEach(function(d, i){
console.log(i, d);
d_keys = Object.keys(d);
console.log(i, d_keys)
values = d[children];
values.forEach(function(v){
d_keys.forEach(function(k){
if (k != children) { v[k] = d[k]}
})
out.push(v);
})
})
return out;
}
console.log("unnested", unnested(nested, "values"));
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment