Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R programming - counting the occurrence of a certain range of numbers

Tags:

range

r

count

Can you guys help get the 'counts' of particular ranges. For example, I want to know how many times a score of 0.5 to 1, 1 to 1.5, 2.0 to 2.5 occurred etc. I have a sample data below.

  Times Scores
    0   0.6
    1.2 0.5
    1.8 1.2
    2.4 1.4
    3   1.5
    3.6 2
    4.2 0.5
    4.8 0.6
    5.4 0.4
    6   1
    6.6 1.1
    8   0.6
    9.4 1.5
    10.8    1.5
    12.2    1.3
    13.6    1.6
    15  1.4
    16.4    1.2
    17.8    1.4
    19.2    2.6
    20.6    2
    22  2.2
    23.4    2.4
    24.8    1.4
    26.2    1.2
    27.6    0.5
    29  0.8
    30.4    1.4
    31.8    1.6
    33.2    2.2
    34.6    2.5
    36  3.6
    37.4    3.78
    38.8    2.5
    40.2    3.5
    41.6    3.4
    43  3.5
    44.4    3.3
    45.8    2.1
    47.2    2.3
    48.6    0.6
    50  0.7
    51.4    0.8
    52.8    1.4
    54.2    1.5
    55.6    1.6
    57  1.5
    58.4    1.2
    59.8    1.1
    61.2    1.56
    62.6    2.02
    64  2.48
    65.4    2.94
    66.8    3.4
    68.2    2.1
    69.6    2.8
    71  3.9
    72.4    2.3
    73.8    2.4
    75.2    5.5
    76.6    4.6
    78  1.2
    79.4    1.3
    80.8    1.4
    82.2    1.8
    83.6    0.5
    85  0.2
    86.4    0.3
    87.8    0.6
    89.2    0.4
    90.6    0.5
    92  0.6
    93.4    1.06
    94.8    1.52
    96.2    1.98
    97.6    2.44
    99  2.9
    100.4   3.36
    101.8   3.82
    103.2   0.6
    104.6   0.5
    106 1.2
    107.4   1.4
    108.8   1.5
    110.2   2
    111.6   0.5
    113 0.6
    114.4   0.4
    115.8   1
    117.2   1.1
    118.6   0.6
    120 1.5
    121.4   1.5
    122.8   1.3
    124.2   1.76
    125.6   2.22
    127 2.68
    128.4   3.14
    129.8   3.6
    131.2   4.06
    132.6   4.52
    134 4.98
    135.4   5.44
    136.8   5.9
    138.2   6.36
    139.6   6.82
    141 0.6
    142.4   0.5
    143.8   1.2
    145.2   1.4
    146.6   1.5
    148 2
    149.4   0.5
    150.8   0.6
    152.2   0.4
    153.6   1
    155 1.1
    156.4   0.6
    157.8   1.5
    159.2   1.5
    160.6   1.3
    162 1.76
    163.4   2.22
    164.8   2.68
    166.2   3.5
    167.6   3.3
    169 2.1
    170.4   2.3
    171.8   0.6
    173.2   0.7
    174.6   0.8
    176 1.4
    177.4   1.5
    178.8   1.6
    180.2   1.5
    181.6   1.2
    183 1.1
    184.4   0.5
    185.8   0.6
    187.2   0.1
    188.6   0.8
    190 0.5
    191.4   0.6
    192.8   0.3
    194.2   0.5
    195.6   1.5
    197 1.8
    198.4   2
    199.8   2.2

I wanted to separate my 'Times' columns into 15 bin intervals which I could do. The code that I used is the one below

Data = cut(Sampledata$Times, seq(0,200, by=15), right=FALSE)
finaldata <- by(Sampledata, Data, FUN=I)  

But now I want to know how to get the counts of different Score ranges for each 15 bin interval The score ranges that I want are : 0.5 to 1, 1 to 1.5, 2.0 to 2.5 for each 15 bin interval

Thanks guys...any suggestion will be much appreciated

like image 345
Sarah May Muholland Avatar asked Jun 25 '13 12:06

Sarah May Muholland


1 Answers

If I understand your question, here is a simple solution:

table(cut(YOURDATA, breaks = seq.int(from = 0.5, to = 2.5, by = 0.5)))

like image 126
J. Kasmanas Avatar answered Oct 09 '22 16:10

J. Kasmanas