Missing last sequence in seq() in R

做~自己de王妃 提交于 2019-12-01 06:43:20

问题


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 which I need to involve in as last value. Is there any straigthforward way how to achieve it? I have tried along.with and length.out parameters but I cannot go trough.


回答1:


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



回答2:


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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/28419281/missing-last-sequence-in-seq-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!