如何为R中的连续向量创建容器?
要为连续向量创建bin,我们可以使用cut函数并将bin与原始向量一起存储在数据框中。cut函数中的值必须根据向量值的范围进行传递,否则bin值中将出现NA。例如,如果我们有一个包含0.55的向量并且我们不在cut函数中使用0,那么第一个bin将是NA。查看以下示例以了解如何正确执行此操作。
示例1
x1<−rnorm(20,5,2) x1
输出结果
[1] 3.066708 6.729915 7.706962 7.564306 3.924779 5.400262 2.529380 4.377311 [9] 7.270613 6.135201 5.068060 6.447229 8.603205 4.065874 4.132155 3.060366 [17] 0.953596 2.861802 7.250666 6.928397
示例
df1<−data.frame(x1,bin=cut(x1,c(0,1,2,3,4,5,6,7,8,9),include.lowest=TRUE)) df1
输出结果
x1 bin 1 3.066708 (3,4] 2 6.729915 (6,7] 3 7.706962 (7,8] 4 7.564306 (7,8] 5 3.924779 (3,4] 6 5.400262 (5,6] 7 2.529380 (2,3] 8 4.377311 (4,5] 9 7.270613 (7,8] 10 6.135201 (6,7] 11 5.068060 (5,6] 12 6.447229 (6,7] 13 8.603205 (8,9] 14 4.065874 (4,5] 15 4.132155 (4,5] 16 3.060366 (3,4] 17 0.953596 [0,1] 18 2.861802 (2,3] 19 7.250666 (7,8] 20 6.928397 (6,7]
例2
x2<−runif(20,2,5) x2
输出结果
[1] 2.656399 2.436808 3.704048 3.572767 2.321280 2.982751 4.911949 2.483126 [9] 2.177203 2.797627 4.621546 3.645550 2.888457 2.919597 4.354709 4.251886 [17] 4.862071 3.367629 2.610280 3.063467
示例
df2<−data.frame(x1,bin=cut(x2,c(2,3,4,5),include.lowest=TRUE)) df2
输出结果
x1 bin 1 3.066708 [2,3] 2 6.729915 [2,3] 3 7.706962 (3,4] 4 7.564306 (3,4] 5 3.924779 [2,3] 6 5.400262 [2,3] 7 2.529380 (4,5] 8 4.377311 [2,3] 9 7.270613 [2,3] 10 6.135201 [2,3] 11 5.068060 (4,5] 12 6.447229 (3,4] 13 8.603205 [2,3] 14 4.065874 [2,3] 15 4.132155 (4,5] 16 3.060366 (4,5] 17 0.953596 (4,5] 18 2.861802 (3,4] 19 7.250666 [2,3] 20 6.928397 (3,4]