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

后端 未结 5 702
小鲜肉
小鲜肉 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:30

    If you are looking for something pre-packaged, consider the freq() function from the descr package.

    library(descr)
    x = c(sample(10:20, 44, TRUE))
    freq(x, plot = FALSE)
    

    Or to get cumulative percents, use the ordered() function

    freq(ordered(x), plot = FALSE)
    

    To add a "cumulative frequencies" column:

    tab = as.data.frame(freq(ordered(x), plot = FALSE))
    CumFreq = cumsum(tab[-dim(tab)[1],]$Frequency)
    tab$CumFreq = c(CumFreq, NA)
    tab
    

    If your data has missing values, a valid percent column is added to the table.

    x = c(sample(10:20, 44, TRUE), NA, NA)
    freq(ordered(x), plot = FALSE)
    

提交回复
热议问题