Extract Consecutive Pairs of Elements from a Vector and Place in a Matrix

前端 未结 2 964
别跟我提以往
别跟我提以往 2020-12-17 07:02

This may be a simple question but I can not find how to produce pairs of values from a vector sequentially which each pair includes last value and new value in a matrix of v

相关标签:
2条回答
  • 2020-12-17 07:11

    This fits your desired output:

    cbind(C[-length(C)], C[-1])
         [,1] [,2]
    [1,]    1   20
    [2,]   20   44
    [3,]   44   62
    [4,]   62   64
    [5,]   64   89
    [6,]   89   91
    [7,]   91  100
    
    0 讨论(0)
  • 2020-12-17 07:34

    How about:

    ## define input    
    C <- c(1 , 20 , 44 , 62 , 64 , 89 , 91, 100)
    ## replicate all but first and last elements
    Crep <- rep(C,c(1,rep(2,length(C)-2),1))
    ## create matrix
    matrix(Crep,ncol=2,byrow=TRUE)
    
    0 讨论(0)
提交回复
热议问题