R: removing the last elements of a vector

后端 未结 6 1317
梦如初夏
梦如初夏 2021-01-31 07:36

How can I remove the last 100 elements of a zoo series?

I know the name[-element] notation but I can\'t get it work to substract a full section

6条回答
  •  轮回少年
    2021-01-31 08:12

    Actually, there's a much faster way:

    y <- x[1:(length(x)-1)]
    

    Code show:

    > microbenchmark( y <- head(x, -1), y <- x[-length(x)],y <- x[1:(length(x)-1)], times=10000)
     Unit: microseconds
                          expr    min      lq     mean  median      uq      max
              y <- head(x, -1) 71.399 76.4090 85.97572 78.9230 84.2775 2795.076
            y <- x[-length(x)] 53.623 55.5585 65.15008 56.5680 61.1585 2523.141
     y <- x[1:(length(x) - 1)] 25.722 28.2925 36.43029 29.1855 30.4010 2410.975
    

提交回复
热议问题