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.
First of all,
seq()
is behaving as it should in your example. You want something thatseq()
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:
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.
Example outputs:
This is a modification of Rich Scriven answer, that does not add an extra number, when it is not necessary (as with
20
)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.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 onYou can place if statement for the last element of the vector such as in the following function :
Then
will return