首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Binning平均值创建直方图

用Binning平均值创建直方图
EN

Stack Overflow用户
提问于 2014-08-09 23:52:06
回答 1查看 1.3K关注 0票数 0

我正在用移动平均法和二进制法制作两个直方图。通过使用excel,我得到了18k个数据点的移动平均值,其中大多数是0值。

这就是我想通过R来完成的

“移动平均”

我想使用R,以便制作一个脚本,将产生一个直方图的多少个‘计数’的设备收到。我试过:

代码语言:javascript
复制
hist(y, 20)  
hist (y, ) 
plot (y, x) 

现在,经过三天的学习,我得到的是:

代码语言:javascript
复制
y <- AltWithAllCounts$Cts.p.ms
x <- AltWithAllCounts$Alt 
barwidth <- 100 
#how many bins
block <- rep(seq(1,length(x)/barwidth),each=barwidth)
#makes bins
a <- aggregate(y,by=list(block),sum) 
#creates sum of bins
altmean <- aggregate(x,by=list(block),mean)
#finds mean altitude of each bin
avgCount <- a$x/barwidth
#averages out each bin
plot(altmean$x,avgCount,xlab="Altitude",ylab="Counts") 
# creates scatterplot of mean bins
 avgBinCnt <- data.frame(altmean$x,a$x)
write.csv(avgBinCnt,file="avgBinCnt.csv",)

我的想法是,我想要20个值的平均值,并随着时间的推移绘制它,也就是x。

代码语言:javascript
复制
x       y
851304  0
851404  0
851503  0
851603  1
851703  0
851804  0
851904  0
852107  0
852203  0
852303  0
922503  0
922603  2
922703  0
922804  0
922904  0
923107  0
923203  0
923303  0
923404  0
923504  0
923604  0
923703  0
923803  0
923904  0
924108  0
924205  1
1441603 0
1441703 0
1441804 0
1441904 0
1442107 1
1442203 1
1442304 0
1442404 4
1442504 0
1442605 1
1442703 6
1442803 8
1442904 0 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-10 00:59:40

直方图显示的是频率,而不是间隔中出现的次数。要得到后者,你可以这样做:

代码语言:javascript
复制
# First create some test data
t <- seq(1,20000)
p <- 2000
s <- (sin(t*pi/p)+1)/2
d <- ifelse(runif(length(s))<s,1,0)
# Each element of d now contains a 1 or a 0, with a probability that varies
# according to the sign function
# Choose how many elements to count over
barwidth <- 100
# Create a vector of block numbers, with each numbered block having a length of 
# barwidth
block <- rep(seq(1,length(s)/barwidth),each=barwidth)
# Now we aggregate with the sum to find the number of 1s in each block
a <- aggregate(d,by=list(block),sum)
# And plot it to show that we have the expected result
barplot(a$x)

..。这意味着:

对于频率散点图而不是计数条形图,这提供了所需的输出:

代码语言:javascript
复制
midpoint <- aggregate(t,by=list(block),mean)
plot(midpoint$x,a$x,xlab="",ylab="frequency")

或者可以找到一个对称的运行平均值,并绘制如下图:

代码语言:javascript
复制
filt <- rep(1/barwidth,barwidth)
y_sym <- filter(d, filt, sides=2)
plot(t,y_sym,xlab="",ylab="frequency")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25224207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档