2013-04-22 33 views
23

有什麼用c()append()之間的區別?有沒有?差()和append()

> c(  rep(0,5), rep(3,2)) 
[1] 0 0 0 0 0 3 3 

> append(rep(0,5), rep(3,2)) 
[1] 0 0 0 0 0 3 3 
+1

'C'也是原始與幾個S3方法(參見'方法( 「C」)') – baptiste 2013-04-22 12:38:31

回答

32

你使用它並不顯示cappend之間差異的方式。 append是在某種意義上說,它允許值到一定位置之後插入到載體不同。

實施例:

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 

如果在R端子輸入append,你會看到它使用c()追加值。

# append function 
function (x, values, after = length(x)) 
{ 
    lengx <- length(x) 
    if (!after) 
     c(values, x) 
    # by default after = length(x) which just calls a c(x, values) 
    else if (after >= lengx) 
     c(x, values) 
    else c(x[1L:after], values, x[(after + 1L):lengx]) 
} 

你可以看到(在評論部分),默認情況下(如果您沒有設置after=在你的例子),它只是返回c(x, values)c是可以連接值vectorslists更通用的功能。

+2

1爲示出了源。總是給人更深入地瞭解R. – Nishanth 2013-04-22 10:38:45

+0

'!after'是真實的'after'是0,否則FALSE。這與之後是否設置無關。 – hadley 2013-04-22 12:05:32