Showing posts with label floor(). Show all posts
Showing posts with label floor(). Show all posts

Wednesday, September 20, 2017

trunc() {base}


trunc() function takes a single numeric argument x and returns a numeric vector containing the integers formed by truncating the values in x toward 0.

trunc(x, ...)

The parameters are:
 -x: numeric vector

x = seq(-3,3, by = 0.5)
x
##  [1] -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5  3.0
trunc(x)
##  [1] -3 -2 -2 -1 -1  0  0  0  1  1  2  2  3
trunc()function returns the integer value most near to 0. For example, if we apply the trunc() function to 3.5 the result will be 3, and if we apply the function to -3.5 the result will be -3 (not -4).

Difference between trunc() and floor() functions:
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.
floor(x)
values = seq(-3,3, by = 0.5)
values
##  [1] -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5  3.0
trunc(values)
##  [1] -3 -2 -2 -1 -1  0  0  0  1  1  2  2  3
floor(values)
##  [1] -3 -3 -2 -2 -1 -1  0  0  1  1  2  2  3
par(mfrow = c(1,2))
x = c(1:13)
y1 = trunc(values)
y2 = floor(values)

plot(x, values,ylim = range(c(-4,4)),col = 'darkturquoise', type = 'l')
points(x, y1, col = 'darkviolet', type = 'l')
legend("topleft", inset=.05, c("No function","Trunc()"), fill=c('darkturquoise','darkviolet'))

plot(x, values,ylim = range(c(-4,4)),col = 'darkturquoise',  type = 'l')
points(x, y2, col = 'deeppink', type = 'l')
legend("topleft", inset=.05, c("No function","Floor()"), fill=c('darkturquoise','deeppink'))
From the plots we see that the function trunc() returns the integer closer to 0 while floor() function returns the integer smaller than the original value.

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.

duplicated() {base}

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