0

I've binned and plotted my data following this example. Now, it was suggested to me that I should add a barplot to the graph showing the amount of data contained in each bin.

How do I sum up the values in the rows for each bin?

Community
  • 1
  • 1
nantoku
  • 59
  • 2
  • 11

1 Answers1

0

I think i understand it now: you want the sum of all values that fit into a bin? You can use tapply for this:

n = 100
x = rnorm(n)
n.breaks = as.integer(sqrt(n))
bins = cut(x, breaks = n.breaks)

sums = tapply(x, bins, sum)
print(sums)

(-2.65,-2.17]    (-2.17,-1.7]    (-1.7,-1.23]  (-1.23,-0.754] (-0.754,-0.281] 
 -7.5825100      -7.6457772      -5.6796399      -8.6823512     -12.8808658 
(-0.281,0.193]   (0.193,0.666]    (0.666,1.14]     (1.14,1.61]     (1.61,2.09] 
 -0.8756864       8.1137694       7.5262578       4.1649094      10.8759823 
Fernando
  • 7,785
  • 6
  • 49
  • 81
  • I looked into it and I don't think it's what I need. If I understand correctly, hist groups by value. I need to group by rows (for example 0 - 20, 21 - 40, 41- 60, ... ) in the same way that the depth.class was created in the example I referred to. – nantoku Oct 09 '13 at 17:23