The trunc() function in R – Things to know

Filed Under: R Programming
Trunc() Function In R

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.


Let’s start with the syntax

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

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.

trunc(-342.56009)
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!

df<-c(123.567,34.00908,8.09889,4.9886,456.0098)
trunc(df)
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.

df<-c(-9.0980,-67.7564,-4563.009098,-11.30976)
trunc(df)
Output = -9   -67   -4563    -11

We got a set of a complete int values here.


The trunc() function with a dataframe

Well, till now we tried passing numbers and vectors and were successful in that. In this section, just push your success a bit by passing a data frame to trunc() function in R.

For this, we are going to use the in-built dataset in R – ‘ability.cov’.

The simple reason for choosing this is, this dataset is filled with float numbers having multiple decimal points.

What are you waiting for? Let’s roll!

#importing dataset
datasets::ability.cov
Trunc In R

You can see the beautiful data present in the data set in the above image. Amazing float values.

Well, let’s pass this dataset to the trunc() function in R and see what happens.

trunc(ability.cov$cov)
The Trunc Function In R

Beautiful int values, isn’t it?

The trunc() function has reduced / returned all the values present in the dataset with zero decimal points.


Wrapping up

The truncate function in R will return the input values with zero decimal points. This statement also means that the function will convert float values into int values as illustrated using multiple examples above.

Well, the only thing I have to say is – all you need is to fall in love with your data, understand it will and care about it.

In R, we have plethora of functions which assist us in our every single step in R. Just get to know about them, understand it’s working and apply as per your requirements.

That’s all for now people. I hope you got better of trunc() function in R. Happy truncating!!!

More study: R documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages