[英]R Fill in empty cells after binning with cut() function
I am using the cut() function to bin a vector integergers. 我正在使用cut()函数对矢量整数进行装箱。 The procedure works well and provides the number of integers that fall into each bin, however for bins without a number, it isn't listed. 该过程运行良好,并提供了每个bin中的整数数量,但是对于没有数字的bin,则不会列出。 I would like to fill these in such that there is a cell for each bin and for bins without numbers, it fills them in with 0. For example: 我想填写这些内容,以便每个垃圾箱都有一个单元格,对于没有数字的垃圾箱,则用0填充它们。例如:
set.seed(123)
x <- sample(1:3, 100, replace = TRUE)
bins <- cut(x, breaks = 4, labels =FALSE)
bins_tab <- table(bins)
bins_tab
This gives: 这给出:
bins
1 3 4
33 34 33
I would like to fill in 2 with a 0 such that the output is: 我想用0填充2,这样输出是:
bins
1 2 3 4
33 0 34 33
Any and all help appreciated. 任何和所有帮助表示赞赏。
Like this? 像这样?
table( cut( x , breaks = 4 , labels = 1:4 ) )
# 1 2 3 4
#33 0 34 33
ie make sure there is a label for the empty factor level. 即确保有一个空因子水平的标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.