55 floor(), ceiling(), round(), and abs()

Written by José Casas and last updated on 7 October 2021.

55.1 Introduction

In this lesson, you will learn how to:

  • Use floor() and ceiling() to get the floor and ceiling of a number, respectively.
  • Use round() to round a number to a specified number of decimal places.
  • Use abs() to compute the absolute value of a number.

Prerequisites:

  • Basic knowledge of R.

All of the following functions can only take numeric or logical (TRUE and FALSE) objects, and NA. These functions also work with vectors of those types.

55.2 floor() and ceiling()

The floor and ceiling of a number are defined as the nearest integer less than and greater than that number, respectively. The floor and ceiling of an integer number are the same number.

Here, we will use floor() and ceiling() to get the floor and ceiling of a single number. For this, simply put a number as the function argument:

floor(3.5)
#> [1] 3
ceiling(7.3)
#> [1] 8
ceiling(-12.8)
#> [1] -12

floor() and ceiling() can also take a vector as an argument:

numbers <- c(2.17, 5.6, 8.0, 9.99, 13.5)

floor(numbers)
#> [1]  2  5  8  9 13

ceiling(numbers)
#> [1]  3  6  8 10 14

55.3 round()

round() lets you round up a number to however many decimal places you want. It takes two arguments: the value to be rounded, and the number of decimal places (called digits in the function).

For example:

round(5.28495, digits = 2)
#> [1] 5.28

round(3.55 * -2.67, digits = 3) # -9.4785
#> [1] -9.478

round() can also take a vector as an argument:

numbers <- c(6.2345, 24.545611, 5, 8.29, 0.00003)

round(numbers, digits = 3)
#> [1]  6.234 24.546  5.000  8.290  0.000

Notice that round() does “round up” after decimals greater than 5, for example:

round(1.345, 2)
#> [1] 1.34
round(1.346, 2)
#> [1] 1.35
round(1.3458, 2)
#> [1] 1.35

round() can also be used to round to a power of ten, by giving a negative number for the second argument. This negative number \(n\) represents the nearest \(n\)-th power of ten. For example, using -2 will round to the nearest hundred.

More examples:

round(132, -2)
#> [1] 100
round(156, -2)
#> [1] 200
round(23, -2)
#> [1] 0
round(689, -3)
#> [1] 1000

55.4 abs()

The absolute value is defined as the distance of a number from the origin of a number line.

With abs() you can calculate the absolute value of a number. For example:

abs(23.1)
#> [1] 23.1
abs(-12)
#> [1] 12

abs() can also take a vector as an argument:

numbers <- c(12, -4, 2.5, -3.77, -.05, NA)
abs(numbers)
#> [1] 12.00  4.00  2.50  3.77  0.05    NA

55.5 Exercises

55.5.1 Question 1

55.5.2 Question 2

Round the following number to the nearest tenth:

55.5.3 Question 3

Calculate the absolute value and round the decimals to 2 digits of the following vector x:

55.5.4 Question 4

55.5.5 Question 5

55.5.6 Question 6

55.5.7 Question 7

55.5.8 Question 8

55.5.9 Question 9

55.5.10 Question 10

55.6 Common Errors

  • non-numeric argument to mathematical function. Remember, all of the functions can only take numeric values, logical objects (TRUE and FALSE), or NA, so if you input something else you will get this error. Make sure that your input doesn’t have some text hiding in there!

55.8 Exercises

55.8.1 Question 1

55.8.2 Question 2

55.8.3 Question 3

55.8.4 Question 4

55.8.5 Question 5

55.8.6 Question 6

55.8.7 Question 7

55.8.8 Question 8

55.8.9 Question 9

55.8.10 Question 10