Issue
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?
Solution
Follow the steps below:
- Create
h1
andh2
, - combine and sort the breaks vectors,
- keep the unique values
- 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.
Answered By - Rui Barradas Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.