Guest User

Untitled

a guest
Oct 15th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.30 KB | None | 0 0
  1. f <- function(x, y){
  2. n <- length(x)
  3. out <- numeric(n)
  4. for(i in seq_along(x)){
  5. if(x[i] %% 2 == 0){
  6. out[i] <- x[i]/y
  7. } else {
  8. out[i] <- x[i]*y
  9. }
  10. }
  11. out
  12. }
  13.  
  14. f = function(x, y) {
  15. evens = (x %% 2) == 0
  16. x[evens] = x[evens] / y
  17. x[!evens] = x[!evens] * y
  18. return(x)
  19. }
Add Comment
Please, Sign In to add comment