assign_bins_vec: Assign bin numbers to a vector

View source: R/binning.R

assign_bins_vecR Documentation

Assign bin numbers to a vector

Description

The first step in binning/down-sampling some data is assigning items to bins. This function takes a vector and a bin size and returns the bin assignments.

Usage

assign_bins_vec(xs, bin_width = 3, na_location = "tail", partial = FALSE)

Arguments

xs

a vector

bin_width

the number of items to put in each bin. Default is 3.

na_location

Where to assign NA bin numbers. "head" and "tail" respectively put the NA elements at the head and tail of the vector; "split" alternates between "tail" and "head".

partial

whether to exclude values that don't fit evenly into bins. Defaults to FALSE, so that the user is warned if a bin is incomplete.

Value

a vector of bin-numbers. If bin_width does not evenly divide xs, the remainder elements are given a bin number of NA.

Examples

## Not run: 
assign_bins_vec(1:14, bin_width = 3, "head")
# [1] NA NA  1  1  1  2  2  2  3  3  3  4  4  4
assign_bins_vec(1:14, bin_width = 3, "tail")
# [1]  1  1  1  2  2  2  3  3  3  4  4  4 NA NA
assign_bins_vec(1:7, bin_width = 5, "split")
# [1] NA  1  1  1  1  1 NA
assign_bins_vec(1:8, bin_width = 5, "split")
# [1] NA  1  1  1  1  1 NA NA

## End(Not run)

tjmahr/littlelisteners documentation built on July 31, 2024, 6:02 a.m.