optimizing loops/vectors in R using C