r apply function to vector