R Apply Function To Each Row Of Matrix