Tuesday, September 19, 2017

floor() ceiling() {base}


ceiling() function: takes a single numeric argument x and returns a numeric vector containing the smallest integers not less than the corresponding elements of x.
floor() function: takes a single numeric argument x and returns a numeric vector containing the largest integers not greater than the corresponding elements of x.

ceiling(x) 
floor(x)

The parameters are: 
-x: a numeric vector

iris$Sepal.Length[1:10] #values of the parameter sepal length
##  [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9
floor(iris$Sepal.Length[1:10]) #values of the parameter sepal length when applying the fucntion floor()
##  [1] 5 4 4 4 5 5 4 5 4 4
ceiling(iris$Sepal.Length[1:10]) #values of the parameter sepal length when applying the fucntion ceiling()
##  [1] 6 5 5 5 5 6 5 5 5 5
x = c(1:10)
y1 = iris$Sepal.Length[1:10]
y2 = floor(iris$Sepal.Length[1:10])
y3 = ceiling(iris$Sepal.Length[1:10])
plot(x, y1,ylim = range(c(3,7)), col = 'darkturquoise', type = 'b', ylab = '10 first values Iris$Sepal.Lenght')
points(x, y2, col = 'darkviolet', type = 'b')
points(x, y3, col = 'deeppink', type = 'b')
legend("topright", inset=.05, title="Function used", c("Ceiling()","No function","Floor()"), fill=c('deeppink', 'darkturquoise','darkviolet'))
With the function ceiling() the values are the first integer higher than the value we are working with,for example if we apply the function ceiling() to 4.5 the result will be 5. 
On the other hand, the function floor() returns the smaller integer than the value we are working with, for example, if we apply the foor() function to the value 4.5 the result will be 4.

No comments:

Post a Comment

duplicated() {base}

duplicated()  function determines which elements are duplicated and returns a logical vector. The parameters of the function are:   ...