R c()和append()之间的差异
使用R c()和append()之间的差异,r,function,R,Function,使用c()和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 您使用它的方式没有显示c和append之间的区别append的不同之处在于,它允许在某个位置之后将值插入向量中 例如: x <- c(10,8,20) c(x, 6) # always adds to the end # [1] 10
c()
和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
您使用它的方式没有显示
c
和append
之间的区别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
您可以看到(注释部分)默认情况下(如果您没有像示例中那样设置
after=
),它只返回c(x,value)
c
是一个更通用的函数,可以将值连接到向量
或列表
c
也是一个具有多种S3方法的原语(请参见方法(“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])
}