Missing last sequence in seq() in R

后端 未结 5 1048
感动是毒
感动是毒 2021-01-13 13:10

I have this example data

by<-200
to<-seq(from=by,to=35280,by=by)

Problem is that to ends at 35200 and ignore the last 80

相关标签:
5条回答
  • 2021-01-13 13:48

    This is a modification of Rich Scriven answer, that does not add an extra number, when it is not necessary (as with 20)

    by <- 2
    out <- 21
    out <- 20
    
    x <- seq(from = 0, to = out + by*ifelse(out %% by>0,1,0) - out %% by , by = by)
    x
    
    0 讨论(0)
  • 2021-01-13 13:49

    First of all, seq() is behaving as it should in your example. You want something that seq() by itself will simply not deliver.

    One solution (there are certainly many) is to check weather "there was anything left" at the end of your sequence and, if so, add another element to it. (Or modify the last element of your sequence, it is not exactly clear what you are trying to achieve.) Like so:

    step <- 200
    end <- 35280
    to<-seq(from=step,to=end,by=step)
    
    # the modulus of your end point by step
    m = end%%step 
    
    # if not zero, you have something to add to your sequence
    if(m != 0) {
    
        # add the end point
        to = c(to, end)
    }
    
    tail(to,2)
    # 35200 35280
    
    0 讨论(0)
  • 2021-01-13 14:00

    You can place if statement for the last element of the vector such as in the following function :

    seqlast <- function (from, to, by) 
    {
      vec <- do.call(what = seq, args = list(from, to, by))
      if ( tail(vec, 1) != to ) {
        return(c(vec, to))
      } else {
        return(vec)
      }
    }
    

    Then

    by <- 200
    to <- seqlast(from=by,to=35280,by=by)
    

    will return

    > head(to)
    [1]  200  400  600  800 1000 1200
    > tail(to)
    [1] 34400 34600 34800 35000 35200 35280
    
    0 讨论(0)
  • 2021-01-13 14:10

    In seq(), "The second form generates from, from+by, ..., up to the sequence value less than or equal to to." And also since 35280 is not in the requested sequence, it is not returned.

    But you can use a calculation in the arguments it you wan to include the next value. Since you know the to value, assign it a name and use it.

    by <- 200
    out <- 35280
    
    x <- seq(from = by, to = (out + by - out %% by), by = by)
    
    length(x)
    # [1] 177
    x[length(x)]
    # [1] 35400
    

    If you want to include the to value, even if it is not in the requested sequence, you can write a little function to add it back on

    seqil <- function(..., include.last = TRUE) {
        x <- do.call(seq.default, list(...))
        if(include.last) c(x, to) else x
    }
    
    by <- 200
    
    x <- seqil(from = by, to = 35280, by = by)
    tail(x)
    # [1] 34400 34600 34800 35000 35200 35280
    
    0 讨论(0)
  • 2021-01-13 14:14

    Whilst not solving the exact issue raised my preferred solution to this is to extend the sequence to add an additional value so that the to value is included in the sequence rather than just appending the to value at the end. This builds on the answers by both @djas and @Etienne Kintzler.

    seq0 <- function(from = 1, to = 1, by = 1, incLast = TRUE){
        out = do.call(what = seq, args = list(from, to, by))
        if (incLast & to%%by != 0){
            out = c(out, tail(out, 1) + by) 
        } 
        return(out)
    }
    

    Example outputs:

    > seq0(from = 0, to = 20, by = 6, incLast = FALSE)
    [1]  0  6 12 18
    > seq0(from = 0, to = 20, by = 6, incLast = TRUE)
    [1]  0  6 12 18 24
    > seq0(from = 0, to = -20, by = -6, incLast = FALSE)
    [1]   0  -6 -12 -18
    > seq0(from = 0, to = -20, by = -6, incLast = TRUE)
    [1]   0  -6 -12 -18 -24
    
    0 讨论(0)
提交回复
热议问题