Floor and ceiling in R is demonstrated with examples in this chapter. floor() function takes the vector or column of the dataframe in R and rounds down those values. ceiling() function takes the vector or column of the dataframe in R and rounds up those values

- floor(x) function in R rounds to the nearest integer that’s smaller than x.
- ceiling(x) function in R rounds to the nearest integer that’s larger than x

**Syntax of floor() function:**

**floor(x)**

- x – numeric value or vector or column of a dataframe to be rounded down

**Syntax of ceiling() function:**

**ceiling(x)**

- x – numeric value or vector or column of a dataframe to be rounded up

#### Example of floor() function in R:

floor() function takes up the value as an argument and rounds down that values without decimal places, so as no decimal values left

# floor function in R floor(125.2395)

**output:**

**[1] 125**

** **

#### Example of floor() function in R for a vector:

floor() function takes up the vector as an argument and rounds down all the values of that vector without decimal places, so as no decimal values left

# floor function in R for vector floor(c(1.234,2.342,4.562,5.671,12.345,14.567))

**output:**

**[1] 1 2 4 5 12 14**

** **

#### Example of ceiling() function in R:

ceiling() function takes up the value as an argument and rounds up that values without decimal places, so as no decimal values left

# ceiling function in R ceiling(125.2395)

**output:**

**[1] 126**

** **

#### Example of ceiling() function in R for a vector:

ceiling() function takes up the vector as an argument and rounds up all the values of that vector without decimal places, so as no decimal values left

# ceiling() function in R for vector ceiling(c(1.234,2.342,4.562,5.671,12.345,14.567))

**output:**

**[1] 2 3 5 6 13 15**

**Example of floor() and ceiling() function in the R dataframe**

First lets create a dataframe

#### Create dataframe in R my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"), Price = c(100.981,80.643,80.223,90.3,65.3,71.9,62.2,71.3,25.1,62.9,41.9,35.7,50.9,121.7)) my_basket

so the resultant dataframe will be

**Examp****le of floor function of column of a dataframe : **

In the below example floor() function is applied to the column of a dataframe in which it takes up the column value as argument. which rounds down the column value without any decimal place.

# floor the decimal places - with R floor() function my_basket$floored = floor(my_basket$Price) my_basket

so in the resultant dataframe “price” column will be rounded down without decimal places and stored in another column named “floored”

**Examp****le of ceiling function of column of a dataframe : **

In the below example ceiling() function is applied to the column of a dataframe in which it takes up the column value as argument. which rounds up the column value without any decimal place.

# ceil the decimal places - with R ceiling() function my_basket$ceiled = ceiling(my_basket$Price) my_basket

so in the resultant dataframe “price” column will be rounded up without decimal places and stored in another column named “ceiled”