youjustreadthis
youjustreadthis

Reputation: 652

R: determining frequency of number in histogram

Assuming I have a histogram with X amount of observations ranging from 0 to 100, with break intervals of 0.00-0.99, 1.00-1.99... etc. how do I associate a new observation (let's say 65.5 for arguments sake), with the number of observations of the appropriate break?

Would using a frequency table rather than a histogram make this easier?

If that wasn't clearly formulated please let me know and I'll try to clarify.

Upvotes: 0

Views: 362

Answers (1)

Andrey Shabalin
Andrey Shabalin

Reputation: 4614

Here is the code doing what you want:

# Setup
data = runif(10000)
h = hist(data, breaks = seq(0,1,length.out = 101))

# New observation
newdata = runif(1)

# Get the bin for the new value
position = findInterval(newdata, h$breaks)

# Extract the counts
counts = h$counts[position]

# Test the counts are correct (for this experiment)
countstest = sum(floor(data*100) == floor(newdata*100))

show(c(counts, countstest))

## [1] 93 93

Upvotes: 3

Related Questions