R LanguageSpeeding up tough-to-vectorize code