Missing last sequence in seq() in R

后端 未结 5 1059
感动是毒
感动是毒 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: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
    

提交回复
热议问题