insert elements in a vector in R

后端 未结 6 1552
悲哀的现实
悲哀的现实 2020-12-01 16:11

I have a vector in R,

a = c(2,3,4,9,10,2,4,19)

let us say I want to efficiently insert the following vectors, b, and c,

b =         


        
6条回答
  •  醉话见心
    2020-12-01 16:33

    The straightforward approach:

    b.pos <- 3
    d.pos <- 7
    c(a[1:b.pos],b,a[(b.pos+1):d.pos],d,a[(d.pos+1):length(a)])
    [1]  2  3  4  2  1  9 10  2  4  0  1 19
    

    Note the importance of parenthesis for the boundaries of the : operator.

提交回复
热议问题