Open In App

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

Improve
Improve
Like Article
Like
Save
Share
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:




# 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:




# 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


Last Updated : 19 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads