cut_interval() 使 n 组具有相等的范围,cut_number() 使 n 具有(大约)相等数量的观察值组; cut_width() 制作宽度为 width 的组。
用法
cut_interval(x, n = NULL, length = NULL, ...)
cut_number(x, n = NULL, ...)
cut_width(x, width, center = NULL, boundary = NULL, closed = "right", ...)
参数
- x
 - 
数值向量
 - n
 - 
要创建的间隔数,或者
 - length
 - 
每个间隔的长度
 - ...
 - 
参数传递给
base::cut.defaultbreaks- 
两个或多个唯一切割点的数值向量或单个数字(大于或等于 2),给出
x要切割成的间隔数。 labels- 
结果类别级别的标签。默认情况下,标签是使用
"(a,b]"间隔表示法构造的。如果labels = FALSE,则返回简单整数代码而不是因子。 right- 
逻辑,指示间隔是否应在右侧关闭(并在左侧打开),反之亦然。
 dig.lab- 
未给出标签时使用的整数。它确定用于格式化中断编号的位数。
 ordered_result- 
逻辑:结果应该是有序因子吗?
 
 - width
 - 
箱子宽度。
 - center, boundary
 - 
指定容器的边位置或中心位置。由于所有 bin 都是对齐的,因此指定单个 bin 的位置(不需要在数据范围内)会影响所有 bin 的位置。如果未指定,则使用“平铺层算法”,并将边界设置为 binwidth 的一半。
以整数为中心,
width = 1和center = 0。boundary = 0.5。 - closed
 - 
"right"或"left"之一指示该箱中是否包含箱的右边或左边。 
例子
table(cut_interval(1:100, 10))
#> 
#>    [1,10.9] (10.9,20.8] (20.8,30.7] (30.7,40.6] (40.6,50.5] (50.5,60.4] 
#>          10          10          10          10          10          10 
#> (60.4,70.3] (70.3,80.2] (80.2,90.1]  (90.1,100] 
#>          10          10          10          10 
table(cut_interval(1:100, 11))
#> 
#>   [1,10]  (10,19]  (19,28]  (28,37]  (37,46]  (46,55]  (55,64]  (64,73] 
#>       10        9        9        9        9        9        9        9 
#>  (73,82]  (82,91] (91,100] 
#>        9        9        9 
set.seed(1)
table(cut_number(runif(1000), 10))
#> 
#> [0.00131,0.105]   (0.105,0.201]   (0.201,0.312]   (0.312,0.398] 
#>             100             100             100             100 
#>   (0.398,0.483]   (0.483,0.596]   (0.596,0.706]   (0.706,0.797] 
#>             100             100             100             100 
#>    (0.797,0.91]        (0.91,1] 
#>             100             100 
table(cut_width(runif(1000), 0.1))
#> 
#> [-0.05,0.05]  (0.05,0.15]  (0.15,0.25]  (0.25,0.35]  (0.35,0.45] 
#>           59          109          103           96          110 
#>  (0.45,0.55]  (0.55,0.65]  (0.65,0.75]  (0.75,0.85]  (0.85,0.95] 
#>           85           89           86          113           97 
#>  (0.95,1.05] 
#>           53 
table(cut_width(runif(1000), 0.1, boundary = 0))
#> 
#>   [0,0.1] (0.1,0.2] (0.2,0.3] (0.3,0.4] (0.4,0.5] (0.5,0.6] (0.6,0.7] 
#>       106       106       108       100        99       107        84 
#> (0.7,0.8] (0.8,0.9]   (0.9,1] 
#>        96        95        99 
table(cut_width(runif(1000), 0.1, center = 0))
#> 
#> [-0.05,0.05]  (0.05,0.15]  (0.15,0.25]  (0.25,0.35]  (0.35,0.45] 
#>           72          104           80          104          100 
#>  (0.45,0.55]  (0.55,0.65]  (0.65,0.75]  (0.75,0.85]  (0.85,0.95] 
#>           91           94           75          115          110 
#>  (0.95,1.05] 
#>           55 
table(cut_width(runif(1000), 0.1, labels = FALSE))
#> 
#>   1   2   3   4   5   6   7   8   9  10  11 
#>  49  92 100  98 112 102  88  89  97 116  57 
相关用法
- R ggplot2 coord_fixed 具有固定“纵横比”的笛卡尔坐标
 - R ggplot2 coord_map Map投影
 - R ggplot2 coord_polar 极坐标
 - R ggplot2 coord_cartesian 笛卡尔坐标
 - R ggplot2 coord_trans 变换后的笛卡尔坐标系
 - R ggplot2 coord_flip x 和 y 翻转的笛卡尔坐标
 - R ggplot2 annotation_logticks 注释:记录刻度线
 - R ggplot2 vars 引用分面变量
 - R ggplot2 position_stack 将重叠的对象堆叠在一起
 - R ggplot2 geom_qq 分位数-分位数图
 - R ggplot2 geom_spoke 由位置、方向和距离参数化的线段
 - R ggplot2 geom_quantile 分位数回归
 - R ggplot2 geom_text 文本
 - R ggplot2 get_alt_text 从绘图中提取替代文本
 - R ggplot2 annotation_custom 注释:自定义grob
 - R ggplot2 geom_ribbon 函数区和面积图
 - R ggplot2 stat_ellipse 计算法行数据椭圆
 - R ggplot2 resolution 计算数值向量的“分辨率”
 - R ggplot2 geom_boxplot 盒须图(Tukey 风格)
 - R ggplot2 lims 设置规模限制
 - R ggplot2 geom_hex 二维箱计数的六边形热图
 - R ggplot2 scale_gradient 渐变色阶
 - R ggplot2 scale_shape 形状比例,又称字形
 - R ggplot2 geom_bar 条形图
 - R ggplot2 draw_key 图例的关键字形
 
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Discretise numeric data into categorical。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
