Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R c()和append()之间的差异_R_Function - Fatal编程技术网

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])
}