In this tutorial, we will look at how to use the built-in floor() function in R with the help of some examples.
What does the floor()
function do in R?
You can use the built-in math function, floor() to get the largest integer smaller than or equal to a given number in R. Pass the number for which you want to get the floor as an argument to the floor()
function. The following is the syntax –
floor(x)
If you pass an integer to the floor()
function, you’ll get the same value as the output. Note that you can apply the floor()
function to a numeric vector, array, matrix, and a dataframe as well.
Examples
Let’s look at some examples of using the floor()
function in R.
Apply floor()
function to a number
First, let’s look at some examples of using the floor()
function on a positive real number.
# floor for a positive real number print(floor(3.2)) print(floor(3.4)) print(floor(3.7))
Output:
[1] 3 [1] 3 [1] 3
You can see that we get the same output, 3 for the values, 3.2, 3.4, and 3.7. Notice that 3 is the largest integer that is smaller than or equal to the above values and thus we get 3 as the output for all the values in the above example.
Let’s now apply the floor function to negative real numbers.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# floor for a negative real number print(floor(-3.2)) print(floor(-3.4)) print(floor(-3.7))
Output:
[1] -4 [1] -4 [1] -4
We get -4 as the result for the numbers, -3.2, -3.4, and -3.7. Here, -4 is the largest integer that is smaller than or equal to values passed.
Apply floor()
function to a numeric vector
You can similarly apply the floor()
function to a numeric vector in R. If you apply the floor()
function to a numeric vector, it will compute the floor for each value in the vector.
Let’s look at an example.
# floor for a numeric vector vec <- c(0, 1.3, -1.3, 2.5, 3.1) print(floor(vec))
Output:
[1] 0 1 -2 2 3
You can see that we get the floor (or the floor value) for each element in the numeric vector.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.