This question already has answers here:
Round up from .5
(7 answers)
Closed 3 years ago.
The help of round states that ‘go to the even digit’ is used for rounding.
How can I have a consistent rounding mechanism?
For example:
This rounds up.
round(1.5, 0)
2
This rounds down.
round(4.5, 0)
4
In both cases I want it to round up.
But I also want 1,4 to round down and 1,7 to round up.
There is the functions floor() and ceiling()
floor(1.5)
1
ceiling(1.5)
2
Related
This question already has answers here:
Rounding numbers to nearest 10 in R
(2 answers)
Closed 1 year ago.
How can I round the following numbers:
16554.2644 and 2238.987
to:
16550 and 2240
and
16560 and 2230
Thanks
Divide the number by 10, round using either floor or ceil and then multiply by 10.
This question already has answers here:
Why (1UL <<53) plus 1.0 does not equal to itself?
(2 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
Why is it that when I put this equation in matlab it equals 0 but when I do (2^52+1) -2^52 it gives me 1, aren't they the same equation. I think I kind of understand how 64bit float can't represent odd numbers past 2^53-1 but these numbers are not even close to that
There are 2^52 double precision numbers between 2^52 and 2^53-1 which the sqrt function maps to (approximately) the interval [2^26, sqrt(2)*2^26]. The latter contains just 2^52/sqrt(2) numbers, which means a lot of numbers x must map to the same sqrt(x).
This question already has answers here:
Calculating the maximum of sub-vectors of a larger vector
(2 answers)
Closed 3 years ago.
I searched at the forums but I could not find an answer for this.
I am looking for a way to convert
c(1,3,4,2,7,12,6,8,15)
to
c(1,3,4,4,7,12,12,12,15) using no loops.
It can be defined as a vector of historical highs of another vector.
This is an example, my data length will be fairly long.
thanks a lot,
Emre
We can use cummax from base R
cummax(v1)
#[1] 1 3 4 4 7 12 12 12 15
data
v1 <- c(1,3,4,2,7,12,6,8,15)
This question already has answers here:
Difference between Math.Floor() and Math.Truncate()
(13 answers)
Closed 5 years ago.
What is the difference between Math.Floor() and Math.Truncate() in .NET?
For example, Math.Floor(4.4) = 4 Math.Truncate(4.4) = 4.
These function behave differently for negative numbers.
Math.Truncate(-4.5) = -4
Math.Floor(-4.5) = -5
Math.Floor rounds down Math.Ceiling rounds up and Math.Truncate rounds towards zero. Thus, Math.Truncate is like Math.Floor for positive numbers, and like Math.Ceiling for negative numbers.
This question already has answers here:
Calculating the maximum of sub-vectors of a larger vector
(2 answers)
Closed 5 years ago.
I am looking for a clever way to transform a vector into a version of itself which runs to its maximum value without ever decreasing, and which plateaus once it reaches its maximum.
So, for input like:
x <- c(1,2,6,3,2,9,8,4)
I'd like output like:
y <- c(1,2,6,6,6,9,9,9)
I find this easy to do with a loop, but I am trying to do this to 1000's of reasonably large vectors, so I'd really prefer a more efficient solution. Thank you in advance!
We can use cummax to do this
cummax(x)
#[1] 1 2 6 6 6 9 9 9