从 hist() 中断为 data.frame
Breaks from hist() into a data.frame
我正在尝试将 hist
中的休息和计数放入 data.frame 中,如下所示:
set.seed(1)
x <- rnorm(10, 3, 2)
data.frame(breaks = hist(x)$breaks, counts = hist(x)$counts)
Error in data.frame(breaks = hist(x)$breaks, counts = hist(x)$counts) :
arguments imply differing number of rows: 7, 6
但是正如您所看到的,这是一个错误,因为它们有不同的 lengths.C 谁能建议一个巧妙的方法来做到这一点?
或者,有没有办法在不使用 hist
的情况下对连续变量进行分箱?
休息总是比计数长一个。因为计数落在每对中断之间。也许您想改为跟踪区域的中点?
with(hist(x), data.frame(breaks = mids, counts = counts))
否则您可以只为区域右边缘的值分配计数
with(hist(x), data.frame(breaks = breaks[-1], counts = counts))
或者您可以跟踪 start/end
with(hist(x), data.frame(embed(breaks,2), counts = counts))
我正在尝试将 hist
中的休息和计数放入 data.frame 中,如下所示:
set.seed(1)
x <- rnorm(10, 3, 2)
data.frame(breaks = hist(x)$breaks, counts = hist(x)$counts)
Error in data.frame(breaks = hist(x)$breaks, counts = hist(x)$counts) :
arguments imply differing number of rows: 7, 6
但是正如您所看到的,这是一个错误,因为它们有不同的 lengths.C 谁能建议一个巧妙的方法来做到这一点?
或者,有没有办法在不使用 hist
的情况下对连续变量进行分箱?
休息总是比计数长一个。因为计数落在每对中断之间。也许您想改为跟踪区域的中点?
with(hist(x), data.frame(breaks = mids, counts = counts))
否则您可以只为区域右边缘的值分配计数
with(hist(x), data.frame(breaks = breaks[-1], counts = counts))
或者您可以跟踪 start/end
with(hist(x), data.frame(embed(breaks,2), counts = counts))