Open In App

Binning a Numeric Vector in R Programming - .bincode() Function

Last Updated : 19 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
.bincode() function in R Language is used to bin a numeric vector and return integer codes for the binning.
Syntax: .bincode(x, breaks, right = TRUE, include.lowest = FALSE) Parameters: x: a numeric vector which is to be converted to integer codes by binning. breaks: a numeric vector of two or more cut points, sorted in increasing order. right: logical value, indicating if the intervals should be closed on the right (and open on the left) or vice versa. include.lowest: logical value, indicating if an ‘x[i]’ equal to the lowest (or highest, for right = FALSE) ‘breaks’ value should be included in the first (or last) bin.
Example 1: Python3
# R program to illustrate
# .bincode function

# Initializing a numeric vector which is
# to be converted to integer
# codes by binning
x <- c(0, 0.01, 0.5, 0.99, 1)

# a numeric vector of two or more cut 
# points, sorted in increasing order
b <- c(0, 1, 2, 3)

# Calling .bincode() function
.bincode(x, b)
.bincode(x, b, TRUE)
.bincode(x, b, FALSE)
Output:
[1] NA  1  1  1  1
[1] NA  1  1  1  1
[1] 1 1 1 1 2
Example 2: Python3
# R program to illustrate
# .bincode function

# Initializing a numeric vector which is
# to be converted to integer
# codes by binning
x <- c(0, 0.1, 0.2, 0.3, 1)

# a numeric vector of two or more cut 
# points, sorted in increasing order
b <- c(0, 1, 2, 3)

# Calling .bincode() function
.bincode(x, b, TRUE, TRUE)
.bincode(x, b, FALSE, TRUE)
Output:
[1] 1 1 1 1 1
[1] 1 1 1 1 2

Next Article
Article Tags :

Similar Reads