Ported python scripts to R. They are slow.