How to generate a frequency table in R with with cumulative frequency and relative frequency

后端 未结 5 704
小鲜肉
小鲜肉 2020-12-12 21:23

I\'m new with R. I need to generate a simple Frequency Table (as in books) with cumulative frequency and relative frequency.

So I want to generate from some simple d

5条回答
  •  感动是毒
    2020-12-12 21:36

    You're close! There are a few functions that will make this easy for you, namely cumsum() and prop.table(). Here's how I'd probably put this together. I make some random data, but the point is the same:

    #Fake data
    x <- sample(10:20, 44, TRUE)
    #Your code
    factorx <- factor(cut(x, breaks=nclass.Sturges(x)))
    #Tabulate and turn into data.frame
    xout <- as.data.frame(table(factorx))
    #Add cumFreq and proportions
    xout <- transform(xout, cumFreq = cumsum(Freq), relative = prop.table(Freq))
    #-----
          factorx Freq cumFreq   relative
    1 (9.99,11.4]   11      11 0.25000000
    2 (11.4,12.9]    3      14 0.06818182
    3 (12.9,14.3]   11      25 0.25000000
    4 (14.3,15.7]    2      27 0.04545455
    5 (15.7,17.1]    6      33 0.13636364
    6 (17.1,18.6]    3      36 0.06818182
    7   (18.6,20]    8      44 0.18181818
    

提交回复
热议问题