R语言 数字向量分档–.bincode()函数
R语言中的 .bincode() 函数用于对一个数字向量进行分选,并返回分选的整数代码。
语法:
.bincode(x, breaks, right = TRUE, include.below = FALSE)
参数:
x: 一个数字向量,要通过分档转换为整数代码。
breaks: 一个由两个或多个切点组成的数字向量,按递增顺序排序。
right: 逻辑值,表示区间是否应在右边关闭(在左边打开)或反之。
include.lower: 逻辑值,表示是否应将等于最低(或最高,对于right = FALSE)的’break’值包括在第一(或最后)个分档中。
例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)
输出
[1] NA 1 1 1 1
[1] NA 1 1 1 1
[1] 1 1 1 1 2
例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)
输出
[1] 1 1 1 1 1
[1] 1 1 1 1 2