Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function in R that can round down OR up to an integer?

Tags:

r

Wondering if there an exists a function that can round a number up or down as I noticed that

as.integer(5.99999)

gives me 5, so it looks like as.integer coerces a numeric to an integer by dropping any number after the decimal place. Thank you!

like image 875
Anna Avatar asked Mar 08 '23 18:03

Anna


1 Answers

The round() function works great. You can do the following to get a result of 6:

as.integer(round(5.99999))

If you want it to round up or down, use the ceiling() or floor() functions, respectively, in place of the round() function. Ex:

as.integer(ceiling(5.9999))
like image 136
Sescopeland Avatar answered Mar 10 '23 07:03

Sescopeland