我有一个1000值的向量,然后我用100个箱子做一个直方图。我想创建一个vector =(Value,Freq),其值为单个值(因此我将有1000个),freq =特定值所在的bin中值的计数。
希望这可以使问题更清楚:
data <- c(6.429229, 9.300965, 11.073744, 6.527263, 8.425178,
6.821384, 6.515991, 9.131452, 6.313888, 8.866572)
Myhist <- hist(data,2)
Myhist$counts
# 5 4 1
# c(5,4,1,5,4,5,5,4,5,4)
MyDF <- cbind(data,c(5,4,1,5,4,5,5,4,5,4))
# Result I want:
# [1,] 6.429229 5
# [2,] 9.300965 4
# [3,] 11.073744 1
# [4,] 6.527263 5
# [5,] 8.425178 4
# [6,] 6.821384 5
# [7,] 6.515991 5
# [8,] 9.131452 4
# [9,] 6.313888 5
#[10,] 8.866572 4
答案 0 :(得分:0)
根据你的例子,我猜你想要的是知道在直方图中落入同一个bin的值的数量(包括有问题的值)。您似乎也指定了直方图中断。
data <- c(6.429229, 9.300965, 11.073744, 6.527263, 8.425178,
6.821384, 6.515991, 9.131452, 6.313888, 8.866572)
get.num.in.bin <- function(data, hist.breaks=2){
Myhist <- hist(data, breaks=hist.breaks, plot=FALSE)
cats <- as.numeric(cut(data, breaks=Myhist$breaks, labels=1:3))
counts <- Myhist$counts[cats]
new.data <- data.frame(data=data, num.in.bin=counts)
return(new.data)
}
get.num.in.bin(data)
# data num.in.bin
# 1 6.429229 5
# 2 9.300965 4
# 3 11.073744 1
# 4 6.527263 5
# 5 8.425178 4
# 6 6.821384 5
# 7 6.515991 5
# 8 9.131452 4
# 9 6.313888 5
# 10 8.866572 4