Using **floor() **and **ceiling()** function in R is easy. In this tutorial, we are focusing on the working and uses of these functions in R.

Hello people, I hope you are doing good. As we know mathematical functions are the key components in the data analysis. Today we are discussing the mathematical functions such as the floor and ceiling in R. Let’s see how these functions will work.

Table of Contents

## Let’s start with the syntax

**Floor():** Floor is a mathematical function in R, which returns the highest integer values which is smaller than the input value i.e. the output value will not be greater than the input value.

```
floor(x)
```

Where:

**x =** input vector or a value.

**Ceiling():** The Ceiling function is also another mathematical function in R that will return the value which is nearest to the input value but it will be greater than the input value.

```
ceiling(x)
```

Where **x =** input vector or a value

**Note:** Both floor() and ceiling()** **values will round of the given input values. But floor function will round off the nearest values which should also be** less than the input value. **In the case of the ceiling function, it rounds off the nearest value which should also be** greater than the input value. **

I know that these definitions may create confusion in your mind. No worries, we are illustrating this with multiple examples for a better understanding as well.

## The floor() function in R

As we discussed above floor() function can return the nearest lesser value of the given input decimal. Let’s see how it works in the below sample.

```
#returns the nearest lesser value for the input
floor(5)
```

**Output = 5**

```
#returns the nearest lesser value for the input
floor(5.9)
```

**Output = 5**

Now, let’s see how we can compute the floor function for the negative value.

```
#returns the nearest lesser value for the input
floor(-5.5)
```

**Output = -6**

In this case, -6 is less than -5.5. Hence the floor() function returned, the output as -6 as you can see above.

As you may observe in the above samples and outputs, the floor() function returns the nearest value which is also less than the given input i.e. 5 and 5.9 the output returned as 5. Because 5 is the nearest value and also it is not greater than input values. For -5.5 the nearest value is -6.

### The floor() function with a vector

In this section, we will be focusing on the application of floor function on the vectors having values in it.

```
#creates a vector
df<-c(1.34,5.67,8.96,4,5.6678,0.00,6.55)
#returns the nearest value which will not be greater than the input values
floor(df)
```

**Output = 1 5 8 4 5 0 6**

Now, let’s see how the floor() function works for the negative values.

```
#creates a vector having nagetive values
df<-c(-3.45,6.7,-0.7,-5.5,4.4,-9.9)
#returns the nearest value which will not be greater than the input values
floor(df)
```

**Output = -4 6 -1 -6 4 -10**

### The floor() function with an expression

In this section we are going to apply the floor function to an expression.

```
#returns the final value which is nearest to input and also less than that.
floor(-3.55 + 2.566 - 8.99 + 3.44 - 5.98)
```

**Output = -13**

As shown above, the floor function will solve the expression or the equation and rounds off the output value.

### The floor() function with a data frame

In this section, we are going to apply the floor() function to a data frame and find the nearest value of those values as well.

Let’s see how it works.

```
#returns the nearest value of the given input
floor(airquality$Wind)
```

**Output = **

```
[1] 7 8 12 11 14 14 8 13 20 8 6 9 9 10 13 11 12 18 11 9 9 16 9 12
[25] 16 14 8 12 14 5 7 8 9 16 9 8 14 9 6 13 11 10 9 8 13 11 14 20
[49] 9 11 10 6 1 4 6 8 8 10 11 14 8 4 9 9 10 4 10 5 6 5 7 8
[73] 14 14 14 14 6 10 6 5 11 6 9 11 8 8 8 12 7 7 7 9 6 13 7 6
[97] 7 4 4 10 8 8 11 11 11 9 11 10 6 7 10 10 15 14 12 9 3 8 5 9
[121] 2 6 6 6 5 2 4 7 15 10 10 10 9 14 15 6 10 11 6 13 10 10 8 12
[145] 9 10 10 16 6 13 14 8 11
```

## ceiling() function in R

As we discussed in the initial phases of this tutorial, the ceiling() function will return the higher integer for the given decimal input.

Let’s see how ceiling works.

```
#returns the nearest value which is greater than the input
ceiling(2.456)
```

**Output = 3**

```
#returns the nearest value which is greater than the input
ceiling(-5.98)
```

**Output = -5**

As you can observe in the above outputs, the ceiling() function is returning the nearest highest value to the given input.

### ceiling() function with a vector

In this section, let’s see how the ceiling function can be applied to a vector having multiple values.

```
#creates a vector
df<-c(1.34,5.66,7.89,9.54)
#returns the nearest value which is greater than the input
ceiling(df)
```

**Output = 2 6 8 10**

For negative values in a vector.

```
#creates a vector
df<-c(-2.34,5.67,-9.87,-6.77)
#returns the nearest value which is greater than the input
ceiling(df)
```

**Output = -2 6 -9 -6**

### ceiling() function with an expression

In this section, we are going to apply the ceiling function to an expression. Let’s see how it works.

```
#returns the nearest value which is greater than the input
ceiling(-3.45 + 5.678 - 7.890 - 4.678)
```

**Output = -10**

```
#returns the nearest value which is greater than the input
ceiling(-3.45 + 5.678 - 7.890 - 4.678 + 6.89000 + 2.456 + 5.678)
```

**Output = 5**

### ceiling() function with a data frame

Well, we have applied the ceiling function to single values, multiple values, vectors, and expressions also. It’s time to apply to the ceiling () function to a data frame. Let’s use the airquality data set for this purpose.

This is the data frame of the airquality data set, which is available in R studio by default.

```
#returns the nearest value which is greater than the input
ceiling(airquality$Wind)
```

**Output =**

```
[1] 8 8 13 12 15 15 9 14 21 9 7 10 10 11 14 12 12 19 12 10 10 17 10 12
[25] 17 15 8 12 15 6 8 9 10 17 10 9 15 10 7 14 12 11 10 8 14 12 15 21
[49] 10 12 11 7 2 5 7 8 8 11 12 15 8 5 10 10 11 5 11 6 7 6 8 9
[73] 15 15 15 15 7 11 7 6 12 7 10 12 9 8 9 12 8 8 8 10 7 14 8 7
[97] 8 5 4 11 8 9 12 12 12 10 12 11 7 8 11 11 16 15 13 10 4 8 6 10
[121] 3 7 7 7 6 3 5 8 16 11 11 11 10 15 16 7 11 12 7 14 11 11 8 13
[145] 10 11 11 17 7 14 15 8 12
```

You can see that the ceiling function returned the nearest highest value of the given input values. Its how the ceiling() function works in R.

## Summing up

R offers the mathematical functions such as the **floor() and ceiling() **to compute the mathematical operations such as finding the nearest values of the input.

In this tutorial, we have discussed the working of floor and ceiling functions. The floor will return the nearest lower value of the given input and on the other side, the ceiling function will return the nearest higher value of the given input as illustrated in the above samples.

That’s all about the floor() and ceiling() functions in R. **Happy flooring and ceiling!!!**

**More study:** Floor in R – R documentation and ceiling in R – stat.ethz