0

I am trying to find out how to calculate percentage of my data for different number ranges. So I have a data that looks like this:

0.81761
0.255319
0.359551
0.210191
0.374046
0.188406
0.179487
0.265152
0.207792
0.202614
0.150943

..and I have these ranges:

0-0.3
0.3-0.7
0.7-1

I want to know out of my data, what is the percentage that fall into a specific number range. So, for example:

0-0.3 -> 72.7%
0.3-0.7 -> 18.18%
0.7-1 -> 9.09%

Does anybody knows how to do this calculation?

  • 1
    "Does anybody knows how to do this calculation?" Office calc would be the tool to use. – Rinzwind Feb 26 '19 at 15:33
  • @steeldriver Thanks for this, but I actually can't find here how to produce percentage for specific number range. I already did make frequency distribution and histograms in excel. What I care is not graph at this point but only a raw number of the percentage within a specific number range. – sergio Feb 26 '19 at 15:41
4

You can use the histogram function from numpy

Ex.

$ python
Python 2.7.12 (default, Nov 12 2018, 14:36:49)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import numpy as np
>>>
>>> data = np.loadtxt('datafile')
>>> hist = np.histogram(data,[0,0.3,0.7,1.0])
>>> print 100.0 * hist[0]/sum(hist[0])
[ 72.72727273  18.18181818   9.09090909]
>>>

See for example NumPy - Histogram Using Matplotlib (of course, you don't have to plot the result).

| improve this answer | |
  • This is far more elegant as my awk solution ;-) – pLumo Feb 26 '19 at 15:51
  • 1
    @RoVo I guess it's like the proverbial swan - numpy is paddling furiously beneath the surface ;) – steeldriver Feb 26 '19 at 15:53
5

Using awk:

awk '
    # Count occurencies
    {
    if ($1 < 0.3) a++
    else if ($1 > 0.7) c++
    else b++
    }
    # Print Percentage of count/NR (num records)
    END {
        printf "< 0.3: %.2f%%\n",a/NR*100
        printf "> 0.3 and < 0.7: %.2f%%\n",b/NR*100
        printf "> 0.7: %.2f%%\n",c/NR*100

    }
' file
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.