Guest User

Untitled

a guest
Dec 10th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.31 KB | None | 0 0
  1. num_seq <- 11:20
  2. # Solution 1: 向量運算
  3. num_seq**2
  4. # Solution 2: apply() 系列函數
  5. sapply(num_seq, FUN = function(x) x**2)
  6. # Solution 3: 迴圈與迭代
  7. seq_length <- length(num_seq)
  8. num_seq_squared <- rep(NA, times = seq_length)
  9. for (i in 1:seq_length) {
  10. num_seq_squared[i] <- num_seq[i]**2
  11. }
  12. num_seq_squared
Add Comment
Please, Sign In to add comment