[英]Converting multiple histogram frequency count into an array in R
For each row in the matrix "result" shown below 对于矩阵“结果”中的每一行,如下所示
A B C D E F G H I J
1 4 6 3 5 9 9 9 3 4 4
2 5 7 5 5 8 8 8 7 4 5
3 7 5 4 4 7 9 7 4 4 5
4 6 6 6 6 8 9 8 6 3 6
5 4 5 5 5 8 8 7 4 3 7
6 7 9 7 6 7 8 8 5 7 6
7 5 6 6 5 8 8 7 3 3 5
8 6 7 4 5 8 9 8 4 6 5
9 6 8 8 6 7 7 7 7 6 6
I would like to plot a histogram for each row with 3 bins as shown below: 我想为每个带有3个bin的行绘制一个直方图,如下所示:
samp<-result[1,]
hist(samp, breaks = 3, col="lightblue", border="pink")
Now what is needed is to convert the histogram frequency counts into an array as follows If I have say 4 bins and say first bin has count=5 and second bin has a count=2 and fourth bin=3. 现在需要将直方图频率计数转换为数组,如下所示:如果我说4个bin,并且说第一个bin的count = 5,第二个bin的count = 2,第四个bin = 3。 Now I want a vector of all values in each of these bins, coming from data result(for every row) in a vector as my output.
现在,我想要这些容器中每个容器中所有值的向量,来自向量中的数据结果(每一行)作为我的输出。
row1 5 2 0 3
For hundreds of rows I would like to do it in an automated way and hence posted this question. 对于数百行,我想以自动化方式进行,因此发布了此问题。
In the end the matrix should look like 最后矩阵应该看起来像
bin 2-4 bin 4-6 bin6-8 bin8-10
row 1 5 2 0 3
row 2
row 3
row 4
row 5
row 6
row 7
row 8
row 9
DF <- read.table(text="A B C D E F G H I J
1 4 6 3 5 9 9 9 3 4 4
2 5 7 5 5 8 8 8 7 4 5
3 7 5 4 4 7 9 7 4 4 5
4 6 6 6 6 8 9 8 6 3 6
5 4 5 5 5 8 8 7 4 3 7
6 7 9 7 6 7 8 8 5 7 6
7 5 6 6 5 8 8 7 3 3 5
8 6 7 4 5 8 9 8 4 6 5
9 6 8 8 6 7 7 7 7 6 6", header=TRUE)
m <- as.matrix(DF)
apply(m,1,function(x) hist(x,breaks = 3)$count)
# $`1`
# [1] 5 2 0 3
#
# $`2`
# [1] 5 0 2 3
#
# $`3`
# [1] 6 3 1
#
# $`4`
# [1] 1 6 2 1
#
# $`5`
# [1] 3 3 4
#
# $`6`
# [1] 3 4 2 1
#
# $`7`
# [1] 2 5 3
#
# $`8`
# [1] 6 3 1
#
# $`9`
# [1] 4 4 0 2
Note that according to the documentation the number of breaks is only a suggestion. 请注意,根据文档,中断次数仅是一个建议。 If you want to have the same number of breaks in all rows, you should do the binning outside of
hist
: 如果要在所有行中使用相同数量的中断,则应在
hist
之外进行装箱:
breaks <- 1:5*2
t(apply(m,1,function(x) table(cut(x,breaks,include.lowest = TRUE))))
# [2,4] (4,6] (6,8] (8,10]
# 1 5 2 0 3
# 2 1 4 5 0
# 3 4 2 3 1
# 4 1 6 2 1
# 5 3 3 4 0
# 6 0 3 6 1
# 7 2 5 3 0
# 8 2 4 3 1
# 9 0 4 6 0
您可以访问hist
返回的counts
向量(有关详细信息,请参见?hist
):
counts <- hist(samp, breaks = 3, col="lightblue", border="pink")$counts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.