rcdfcumulative-frequency

How to generate empirical c.d.f from a set of observations?


Assume that I have a vector x = c(1, 1, 3, 0, 4, 5, 4). I would like to ask if there is a function to generate the of this data. My desire result is

               x    c.d.f
1              0      1/7
2              1      3/7
3              3      4/7
4              4      6/7
5              5      7/7

I tried function ecdf(c(1, 1, 3, 0, 4, 5, 4)), but I don't understand the resulted value of this function

Empirical CDF 
Call: ecdf(c(1, 1, 3, 0, 4, 5, 4))
 x[1:5] =      0,      1,      3,      4,      5

Could you please help me generate this c.d.f? Thank you so much!


Solution

  • ecdf returns a function. You get your desired output using

    Fn <- ecdf(x)
    out <- data.frame(x = knots(Fn), cdf = Fn(knots(Fn)))
    out
    #  x       cdf
    #1 0 0.1428571
    #2 1 0.4285714
    #3 3 0.5714286
    #4 4 0.8571429
    #5 5 1.0000000