The trunc() function in R is more often used by the programmers to round the values to zero decimal points. In other words, you can convert float values into an int using trunc() function. You can also call it as truncate() function.

The word truncate means, resize, or reduce something. In our case, reducing the decimal points to null.

Hey people, you may know that the round function in R also does the same. But the round function will round off the values to the nearest point.

The trunc() function will reduce the decimal values to zero and able to convert float values into int.


Table of Contents[

hide

]

Let’s start with the syntax

trunc(): Function which reduces the input float numbers to zero decimal points.

1

trunc``(x)

x = An input string, vector or a dataframe.


The trunc() function in R

Well, in this section, let’s try to compute the trunc() value of a string.

trunc``(989.5678)

Output = 989

In the above section, you can see that the trunc() function in R reduced the input values to zero decimal place and converted it into an int number.

Let’s compute the trunc values of a negative number in the below section.

1

trunc``(-342.56009)

1

Output =  -342

The trunc() function will return the negative number with zero decimal points. cool right?


The turnc() function with a vector

In this section, let’s pass a vector having various values to see how it computes the data. Let’s roll!

1

2

df<-``c``(123.567,34.00908,8.09889,4.9886,456.0098)

trunc``(df)

1

Output =  123   34   8    4   456

Well, the trunc() function has returned the int values with zero decimal points. Don’t stop, try to pass a negative vector.

#r programming #function

The trunc() function in R - Things to know - JournalDev
1.30 GEEK