Reputation: 235
Could someone tell me how to average two histograms with R?
I came across the HistogramTools package and the AddHistograms
function:
h1<-hist(na.omit(a[,1]),breaks = 100,plot=F)
h2<-hist(na.omit(a[,2]),breaks = 100,plot=F)
> AddHistograms(h1,h2)
Error in .AddManyHistograms(x, main = main) :
Histograms must have identical breakpoints for this operation.
but I always have the same error Histograms must have identical breakpoints for this operation
? can someone explain why?
I am guessing is that a[,1] and a[,2] are not the same length, same with the outputs of h1 and h2 (i.e. I don't have the same length for "breaks","mids","counts" between h1 and h2).
Could you tell me how to average my two histograms using this function or anything else with R?
Upvotes: 0
Views: 286
Reputation: 76402
Follow the steps below:
h1
and h2
,With the (not reproducible) example in the question,
h1 <- hist(na.omit(a[,1]), plot = FALSE)
h2 <- hist(na.omit(a[,2]), plot = FALSE)
brks <- sort(c(h1$breaks, h2$breaks))
brks <- unique(brks)
h1 <- hist(na.omit(a[,1]), breaks = brks, plot = FALSE)
h2 <- hist(na.omit(a[,2]), breaks = brks, plot = FALSE)
h12 <- AddHistograms(h1, h2)
plot(h12)
Note also that na.omit
is not really needed, hist
will discard them anyhow.
Upvotes: 1