From Excel To R R

Round, roundup, rounddown, trunc in R

rounding numbers, dates and time in R

Here is how to round or truncate numbers, dates, and times in R in different ways. Depending on your goals and situation, there might be different needs and efficient solutions.

Round numbers in R

You can do it by using the round function and specifying how many decimal places you want to keep.

round(1.27, digits = 1)
#[1] 1.3

Rounding in R is simple, but the result might not be what you expect in a specific situation. For example, if you have to round 0.5. As a result of the ROUND function in Excel, there will be 1, but in R, it is different.

round(0.5)
#[1] 0

In the documentation of round function is an explanation.

“Note that for rounding off a 5, the IEC 60559 standard (see also ‘IEEE 754’) is expected to be used, ‘go to the even digit’. Therefore round(0.5) is 0 and round(-1.5) is -2.”

If you accept rounding to the even digit, then there is no problem.

 

R round half up like in Excel

Here is one of the best solutions to round half up in R.

round.off <- function (x, digits=0) 
{
  posneg = sign(x)
  z = trunc(abs(x) * 10 ^ (digits + 1)) / 10
  z = floor(z * posneg + 0.5) / 10 ^ digits
  return(z)
}


round.off(0.5)
#[1] 1

If you want to use this function frequently, then you can customize your R profile. Here is an example of how to do that and add your function to the namespace of existing packages.

 

Round to nearest 10, 100, 1000, etc

A lesser-known feature or round function is rounding to a negative number of digits. -1 means rounding to the nearest 10, -2 means rounding to the nearest 100, etc.

round(123, digits = -1)
#[1] 120

 

Roundup or round down numbers in R

To round up, use ceiling, and to round down, use the floor. Both functions round to the nearest integer but in a different direction.

ceiling(1.27)
#[1] 2

floor(1.27)
#[1] 1

 

More complex rounding, round up or down to nearest multiple

One of the simplest solutions is to use round_any() from the plyr package that can do complex math by specifying necessary parameters. You can do all that is not straightforward possible with the base functions round, ceiling, floor.

For example, if you want to round up to the nearest 10 it looks like this.

plyr::round_any(123, 10, f = ceiling)
#[1] 130

If you want to round down to the nearest 5 it looks like this.

plyr::round_any(18, 5, f = floor)
#[1] 15

If you want to round just to the nearest 5, then here it is.

plyr::round_any(18, 5, f = round)
#[1] 20

plyr::round_any(12, 5, f = round)
#[1] 10

Round down but by keeping 2 decimal places.

plyr::round_any(18.129, 0.01, f = floor)
#[1] 18.12

 

Truncate in R

If you want to get rid of decimal numbers, then it is simple by using the trunc function.

trunc(1.27)
[1] 1

In the Excel, TRUNC function can leave necessary decimal numbers, but in R, the same is done by working with numbers as text.

as.numeric(substr(as.character(1.27), 1, 3))
#[1] 1.2

Lesser-known feature truncated division or, in other words, integer division.

9 %/% 4
#[1] 2

If you want to truncate the time component in the date time, then here is how to do that in R.

 

Round date and time in R

It is easy to use functions in the lubridate package like round_date, floor_date, ceiling_date that are useful to round date and time where it is necessary.

For example, if you want to round down to the nearest 15 minute time interval, then it looks like this.

strftime(floor_date(as.POSIXct(Sys.time()), '15 minutes'), format = '%Y-%m-%d %H:%M:%S')
#[1] "2021-08-10 19:00:00"

Here is an example of how to round to last fully closed time interval.

You can also do the rounding with dates. For example, you can get the start date of the actual month or next month.

lubridate::ceiling_date(Sys.Date(), unit = "month")
#[1] "2021-09-01"

lubridate::floor_date(Sys.Date(), unit = "month")
#[1] "2021-08-01"

Here are other examples of how you can use date rounding to get necessary dates in R.

 




0 comments on “Round, roundup, rounddown, trunc in R

Leave a Reply

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