centerBin: Cut a set of numeric data into bins based on the middle value...

View source: R/centerBin.R

centerBinR Documentation

Cut a set of numeric data into bins based on the middle value of the bin

Description

This function cuts a vector of numeric data such that the breaks are set using the middle value of a given bin rather than setting upper or lower boundaries for each bin. For example, say you've got a vector of blood draw times that weren't drawn at exactly the nominal time but were close to it. Set the bins here to the nominal times, and the data will be filed into the slot that's closest to the nominal time at which that sample should have been drawn. For a similar option, see cutNumeric or see the base R function cut.

Usage

centerBin(x, breaks)

Arguments

x

A numeric string

breaks

the desired breaks in that string set by what the middle value should be. Any value of x exactly in the middle of two breaks will be assigned to the larger break, i.e., the function rounds up.

Value

Returns a vector of numeric data

Examples

x <- c(48, 39, 28, 24, 5, 64, 133, 51, 59, 92, NA, 39)
centerBin(x, breaks = seq(0, 340, 5))
centerBin(x, breaks = seq(0, 340, 10))

# Let's look at an example where the times in a PK study weren't perfect.
ActualTime <- c(-5, 17, 32, 65, 118)
NominalTime <- c(0, 15, 30, 60, 120)
centerBin(ActualTime, breaks = NominalTime)



shirewoman2/LaurasHelpers documentation built on Oct. 22, 2023, 2:07 p.m.