Truncate function in R – trunc()

trunc(x) is a truncate function in R, which rounds to the nearest integer in the direction of 0. trunc() function basically truncates the values in the decimal places. trunc() function is used in truncating  the values of vector and truncating the values of a column in R. Lets see an example for each.

  • trunc() function to truncate the values in R.
  • truncate the values of vector using trunc() function in R
  • truncate the column in R dataframe using trunc() function

 

Syntax of trunc() function:

trunc(x)
  • x – numeric value or vector or column of a dataframe to be truncated

 

Example of trunc() function in R:

trunc() function takes up the value as an argument and truncates that values in the decimal places, so as no decimal values left

# truncate function in R

trunc(125.2395)
output:
[1] 125

 

 

Truncating a negative value:

trunc() function takes up the negative value as an argument and truncates that values in the decimal places, so as no decimal values left

# truncate function in R for negative decimal value

trunc(-125.2395)

As truncate function in R, rounds the nearest integer in direction of 0, the output will be -125

output:
[1] -125

 

 

Example of trunc() function in R for a vector:

trunc() function takes up the vector as argument and truncates all the values so that no decimal place is left.

# truncate function in R for a vector

trunc(c(1.234,2.342,-4.562,5.671,12.345,-14.567))
output:
[1]   1   2  -4   5  12 -14

 

 

Example of trunc() function in R of the dataframe column:

First lets create a dataframe

#### Create dataframe in R

my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), 
                       ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"),
                       Price = c(100.981,80.643,80.223,90.3,65.3,71.9,62.2,71.3,25.1,62.9,41.9,35.7,50.9,121.7))

my_basket
 

so the resultant dataframe will be

round function in R 11

 

 

Example of trunc function of column of a dataframe : 

In the below example trunc() function is applied to the column of a dataframe in which it takes up the column value as argument. which truncates the column value without any decimal place.

# truncate the decimal places - with R trunc() function

my_basket$truncate= trunc(my_basket$Price)
my_basket

so in the resultant dataframe “price” column will be truncated without decimal places and stored in another column named “truncate”

Truncate function in R TRUNC() 11

 

previous small truncate function in r                                                                                                           next small truncate function in r

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.