Floor and ceiling in R

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

round function in R 11

 

 

Example 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”

Ceiling and floor in R - ceil() and floor() function 11

 

 

Example 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”

Ceiling and floor in R - ceil() and floor() function 12

previous small floor and ceiling in r                                                                                                           next small floor and ceiling in r