Category: programming

Chicago Murder Rate and Leaflet for R

Recently got hooked on Leaflet for R which is an amazing library that allows for interactive Javascript maps to be generated within R. Still have a bit to explore, but the way it handles layering on a map is really nice. I plan on writing a few UI extensions as well, since there is some interactivity I would love to add (sliders without server calls being the biggest).

Chicago Murder Rate by Year
Chicago Murder Rate by Year

Everyone was talking about the Chicago crime rates, so I figured I would give the dataset a go, and came up with the above animation. Each frame was a subset of the murder rates for that frame’s year. Years go from 2001 – 2016, and I will open up the code once I improve the UI.

Find Minimum Distance Between List of Points

This is a neat little call I whipped up that returns the two x and y points from a list which have the shortest distance. It also returns the distance between those two points.

Pretty weird solution right? Seems to work though!

Flatten Data in R by Column Values

Not sure if this will help anyone, but I recently wrote a script that will let you flatten/widen a data set based on a dynamically set column name.

Animal Instances Instances Instances Instances
Puppy 47 77 97 25
Puppy 26 6 14 98
Kitten 38 50 5 73
Puppy 83 61 43 4
Kitten 78 30 57 8

…would turn into this

V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
Puppy 47 77 97 25 26 6 14 98 83 61 43 4
Kitten 38 50 5 73 78 30 57 8

Breaks a vector into defined segment lengths

Another gist written for stack overflow. This breaks up a vector defined lengths and accounts for the remainder size. Great for splitting up a vector for saving into different files. It is a loop, but any performance issues should be minimal and increases readability/extendability.

Find Patterns in R Vector

Wrote this while providing a solution on Stack Overflow. Figured it might actually come in handy so I saved it. Not the most elegant, and not vectorized, but it might be useful to others as well.

Dynamically Set Accordion Navigation

One of my friends who works for a fairly large company (loooooots of internal links) needed a way to structure the left navigation in such a way that the links would take you to the page, but the list element/clickable element would keep you on the page and expand/contract the navigation. There weren’t any good solutions around that he could find, so I decided to help him out.

This solution is not only an accordion for the menu, but dynamically adds indicators to what list elements are a dropdown and since it makes the list element handle the navigation menu, any element in the list element (including ::before content) will let you expand or collapse the menu system. Clicking on a link will take you to the page, and cancel any accordion effects.

See the Pen VYraBx by Matt Sandy (@MattSandy) on CodePen.

Hopefully this helps someone else out there as well.

Threaded PHP Download

I wrote this code for my bandcamp download project which has since been retired. It breaks up a download into different chunks and saves them to chunk.part1, chunk.part2, etc. I was pretty happy since it sped up my downloads a lot.

Return a Date Range Array in PHP

I think this code might come in handy for me as well as other programmers. It lets you pass two dates and returns an array containing the range of dates between them. I had found code previously, but there was a glitch and so I decided to just write my own.

Numeric equivalent of an Excel column name

I wrote this little php function to see how small I could make the code. I think I did pretty well and figured I would post it here as to not forget in case I needed to actually use it some day. There might be a better way, but I had some fun with this.

function b($b){$l=strlen($b);while($i<$l){$c+=(base_convert($b[$i],36,10)-9)*pow(26,$l-1-$i);$i++;}return $c;}