0

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?

2
  • Maybe you can try hist(c(a[,1],a[,2])), but without a reproducible example it's hard to tell. You should also specify what you intend by "average two histograms". Commented Sep 10, 2021 at 15:32
  • 2
    Could you please provide some data? Did you try to set an identical vector of breakpoints VectorOfBreakpoints <- c(0, ..., ..) to each histogram, like breaks = VectorOfBreakpoints? Commented Sep 10, 2021 at 15:33

1 Answer 1

1

Follow the steps below:

  1. Create h1 and h2,
  2. combine and sort the breaks vectors,
  3. keep the unique values
  4. and add the histograms.

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.