Search code examples
r

Remove the first and last element of a vector of unknown length


I have a vector of "yearmon" and I want to strip the first and the last elements of it while creating it.

Kind of smt like this:

vec_strp <- seq(start, end, by = "month")[-first, - last]

Solution

  • Maybe this:

    #Code
    vec_strp <- seq(start, end, by = "month")[-c(1,length(seq(start, end, by = "month")))]
    

    Some testing:

    #Data
    start <- as.Date('2020-01-01')
    end <- as.Date('2020-12-31')
    #Code
    vec_strp <- seq(start, end, by = "month")[-c(1,length(seq(start, end, by = "month")))]
    

    Output:

     [1] "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" "2020-06-01" "2020-07-01" "2020-08-01"
     [8] "2020-09-01" "2020-10-01" "2020-11-01"