Difference between c() and append()
append() can be used instead of c(), but it is not recommended as c() is much more rapid than append():
> microbenchmark({m <- c(0, c(0, 3))}, times = 1E6)
Unit: nanoseconds
expr min lq mean median uq max neval
{ m <- c(0, c(0, 3)) } 288 349 450.4081 355 393 6536440 1e+06
> microbenchmark({m <- append(0, append(0, 3))}, times = 1E6)
Unit: microseconds
expr min lq mean median uq max neval
{ m <- append(0, append(0, 3)) } 1.488 1.763 2.178313 1.868 2.033 12825.54 1e+06
However, append() has one option more:
x <- c(10,8,20)
c(x, 6) # always adds to the end
# [1] 10 8 20 6
append(x, 6, after = 2)
# [1] 10 8 6 20
Commentaires
Enregistrer un commentaire