0

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!

massisenergy
  • 1,764
  • 3
  • 14
  • 25
Akira
  • 2,594
  • 3
  • 20
  • 45

2 Answers2

2

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
markus
  • 25,843
  • 5
  • 39
  • 58
  • See also: https://stats.stackexchange.com/questions/30858/how-to-calculate-cumulative-distribution-in-r – markus Mar 19 '20 at 19:23
2

The ecdf function returns a function. To get your desired result, call the resulting function on the original vector.

x <- c(1, 1, 3, 0, 4, 5, 4)

Fn <- ecdf(x)
class(Fn)
#> [1] "ecdf"     "stepfun"  "function"
Fn(x)
#> [1] 0.4285714 0.4285714 0.5714286 0.1428571 0.8571429 1.0000000 0.8571429

Created on 2020-03-19 by the reprex package (v0.3.0)

11rchitwood
  • 25
  • 1
  • 6