1

I have a vector of 1000 value, I then make a histogram with 100 bins. I want to make a vector = (Value, Freq) with the value being the individual value (so I will have 1000 of them) with the freq = the count of the values in the bin that particular value falls into.

Hopefully this can make problem clearer:

data <- c(6.429229, 9.300965, 11.073744, 6.527263, 8.425178, 
          6.821384, 6.515991,  9.131452, 6.313888, 8.866572) 
Myhist <- hist(data,2)
Myhist$counts
# 5 4 1

# c(5,4,1,5,4,5,5,4,5,4)
MyDF <- cbind(data,c(5,4,1,5,4,5,5,4,5,4))
# Result I want: 

# [1,]  6.429229 5
# [2,]  9.300965 4
# [3,] 11.073744 1
# [4,]  6.527263 5
# [5,]  8.425178 4
# [6,]  6.821384 5
# [7,]  6.515991 5
# [8,]  9.131452 4
# [9,]  6.313888 5
#[10,]  8.866572 4
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
Ashton
  • 13
  • 5
  • 2
    I don't quite follow this. Can you provide a [reproducible example](http://stackoverflow.com/q/5963269/1217536) for people to work with? If you have 1000 unique losses, then you will have 1k bins needed & the count of observed losses within each bin will be 1. – gung - Reinstate Monica Jun 24 '16 at 19:18
  • 1
    Can you give us an example of input and expected output, of course reducing the size of the input (e.g.10 losses, 5 bins) – digEmAll Jun 24 '16 at 19:18
  • Hey sorry I have added a basic example hope this makes it clearer – Ashton Jun 24 '16 at 19:39

1 Answers1

0

Based on your example, I'm guessing what you want is to know the number of values (including the value in question) that fall into the same bin in a histogram. You also appear to have specified the histogram breaks.

data <- c(6.429229, 9.300965, 11.073744, 6.527263, 8.425178, 
          6.821384, 6.515991,  9.131452, 6.313888, 8.866572)

get.num.in.bin <- function(data, hist.breaks=2){
  Myhist   <- hist(data, breaks=hist.breaks, plot=FALSE)
  cats     <- as.numeric(cut(data, breaks=Myhist$breaks, labels=1:3))
  counts   <- Myhist$counts[cats]
  new.data <- data.frame(data=data, num.in.bin=counts)
  return(new.data)
}
get.num.in.bin(data)
#         data num.in.bin
# 1   6.429229          5
# 2   9.300965          4
# 3  11.073744          1
# 4   6.527263          5
# 5   8.425178          4
# 6   6.821384          5
# 7   6.515991          5
# 8   9.131452          4
# 9   6.313888          5
# 10  8.866572          4
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79