nik
nik

Reputation: 2584

How to compute frequency distribution in R?

Actually there are many functions which I don't know what to use to calculate it

X = rnorm(100)

Lets assume I have a normally distributed vector called X. I don't know anything about bin centre and I want to calculate the frequency distribution for X

As an output I want to have one vector of frequency and one vector as bin Centres I think calculating bin centre can be done like

seq(min(mat[,1]),max(mat[,1]), by=0.01 ) 

Can you please guide me to calculate it and obtain the needed output? which function I must use ?

example data

0.0014985
0.0087414
0.011304
0.011619
0.013201
0.017573
0.018898
0.022491
0.023001
0.024195
0.024434
0.024469
0.028859
0.031385
0.031647
0.032224
0.033946
0.036938
0.037867
0.037993
0.038889
0.038922
0.03894
0.041474
0.041804
0.044066
0.044082
0.044962
0.047495

With Prism I obtain the following (bin width is 0.05)

bin centres   frequency
    0           1
    0.005       0
    0.01        3
    0.015       1
    0.02        3
    0.025       4
    0.03        4
    0.035       2
    0.04        7
    0.045       4
    0.05        0

Upvotes: 0

Views: 1792

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50728

You can use R’s hist function (see ?hist):

h <- hist(X);

The bin centres are given by

h$mids

and the counts by

h$counts

Normalised counts are then simply h$counts / sum(h$counts).

You can specify the bins using the breaks argument (either by specifying the number of bins, or by given the bins directly).

By the way, the same can be achieved using R’s cut and table functions (see ?cut, ?table).

Upvotes: 2

Related Questions