Missing last sequence in seq() in R

2020-03-24 09:12发布

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.

标签: r seq
5条回答
姐就是有狂的资本
2楼-- · 2020-03-24 09:26

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
查看更多
甜甜的少女心
3楼-- · 2020-03-24 09:26

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
查看更多
孤傲高冷的网名
4楼-- · 2020-03-24 09:27

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
查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-03-24 09:36

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
查看更多
叼着烟拽天下
6楼-- · 2020-03-24 09:48

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
查看更多
登录 后发表回答