user971956
user971956

Reputation: 3208

How to calculate the percentage of data points belonging to a range of values?

Given a table of values (say between 0 to 100) and the attached plot, what would be the simplest way using R to calculate how many of the data points fall between values 20 - 60 (the red box in the image)?

And is there a way to create that red box using R's plotting functions (I did it using a image editor...)?

Thanks for the help. enter image description here

Upvotes: 4

Views: 9675

Answers (2)

Josh O'Brien
Josh O'Brien

Reputation: 162371

To calculate the probability mass contained within the interval:

x <- rnorm(1e6)  ## data forming your empirical distribution
ll <- -1.96      ## lower bound of interval of interest
ul <- 1.96       ## upper bound of interval of interest

sum(x > ll & x < ul)/length(x)
# [1] 0.949735

And then to plot the histogram and the red box:

h <- hist(x, breaks=100, plot=FALSE)       # Calculate but don't plot histogram
maxct <- max(h$counts)                     # Extract height of the tallest bar
## Or, if you want the height of the tallest bar within the interval
# start <- findInterval(ll, h$breaks)
# end   <- findInterval(ul, h$breaks)
# maxct <- max(h$counts[start:end])

plot(h, ylim=c(0, 1.05*maxct), col="blue") # Plot, leaving a bit of space up top

rect(xleft = ll, ybottom = -0.02*maxct,    # Add box extending a bit above
     xright = ul, ytop = 1.02*maxct,       # and a bit below the bars
     border = "red", lwd = 2)

enter image description here

Upvotes: 13

Roland
Roland

Reputation: 132736

set.seed(42) 
x <- rlnorm(5000) #some data
hist(x) #histogram
rect(7,-50,10,100,border="red") #red rectangle
table(cut(x,breaks=c(0,7,10,Inf)))/length(x) #fraction of values in intervals
#(0,7]    (7,10]   (10,Inf] 
#0.9754   0.0136   0.0110 

Cut categorizes the values according to which interval they belong in. table then creates a table of counts, which then can be divided by the total count length(x).

Upvotes: 8

Related Questions