roundp: Round values to a given precision

Description Usage Arguments Value Examples

Description

Round values to a given precision

Usage

1
roundp(x, precision, f = round)

Arguments

x

vector of values, can be numeric, POSIXct, Date.

precision

precision at which to round, in the unit of x (for POSIXct, seconds; for Date, days).

f

rounding function: base::floor(), base::ceiling(), and , base::round()

Value

A vector of the same length as x, rounded to precision.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
roundp(1:10, 1)
roundp(1:10, 4)
roundp(1:10, 4, floor)
roundp(1:10, 4, ceiling)

roundp(Sys.time() + 1:5, 3)
roundp(Sys.time() + 1:5, 5, floor)
# round on the hour
roundp(Sys.time(), 3600)

roundp(Sys.Date() + 1:5, 2)
roundp(Sys.Date() + 1:5, 2, floor)

# useful to bin data over a coordinate (or time)
library("dplyr")
d %>%
  # bin depth over 10m
  mutate(bin=roundp(depth, 10)) %>%
  # average each variables in each bin
  group_by(bin) %>% summarise_all(mean)

jiho/castr documentation built on April 5, 2020, 2:12 p.m.