Ilya - 7 months ago 69

R Question

I need a decimal variant of

`ceiling`

`floor`

`signif`

For example, I need the number 2.63 to be transformed into 2.7, and not 3 (

`ceiling`

`signif(2.63,2)`

The only solutions I can think of is to multiply by 10, then take

`ceiling`

`ceiling(2.63*10)/10`

I'm sure, there must be a more elegant solution.

My questions is quite similar to this one.

Answer

@42- answer in comment seems too useful to be just left in comment, hence this wiki-post.

You can create your own functions and adapt `floor`

and `ceiling`

fonctions, adding a `level`

option to mimic the `digits`

parameter of `round`

function like this:

```
floor_dec <- function(x, level=1) round(x - 5*10^(-level-1), level)
ceiling_dec <- function(x, level=1) round(x + 5*10^(-level-1), level)
```

**Examples:**

```
# floor
floor_dec(1.259, 2)
[1] 1.25
floor_dec(1.9, 0)
[1] 1
floor_dec(29, -1)
[1] 20
floor_dec(1.251, 2)
[1] 1.25
floor_dec(1.2, 0)
[1] 1
floor_dec(23, -1)
[1] 20
# ceiling
ceiling_dec(1.259, 2)
[1] 1.26
ceiling_dec(1.9, 0)
[1] 2
ceiling_dec(29, -1)
[1] 30
ceiling_dec(1.251, 2)
[1] 1.26
ceiling_dec(1.2, 0)
[1] 2
ceiling_dec(23, -1)
[1] 30
```