Frequency Distribution Table

user3513178 picture user3513178 · Apr 9, 2014 · Viewed 15.2k times · Source

I am relatively new to [R] and am looking for the best way to calculate a frequency distribution from a vector (most likely numeric but not always) complete with the Frequency, Relative Frequency, Cumulative Frequency, Cumulative Relative Frequency for each value. Below is the logic I came up with, but it seems like a bit much for such a routine task. I appreciate your feedback.

x <- c(1,2,3,2,4,2,5,4,6,7,8,9)

freq <- data.frame(table(x))

relFreq <- data.frame(prop.table(table(x)))
relFreq$Relative_Freq <- relFreq$Freq
relFreq$Freq <- NULL

Cumulative_Freq <- cumsum(table(x))

z <- cbind(merge(freq, relFreq), Cumulative_Freq)
z$Cumulative_Relative_Freq <- z$Cumulative_Freq / sum(z$Freq)

print(z)

Answer

Tyler Rinker picture Tyler Rinker · Apr 9, 2014

The qdap package contains dist_tab to do this:

library(qdap)
dist_tab(x)

##   interval Freq cum.Freq percent cum.percent
## 1        1    1        1    8.33        8.33
## 2        2    3        4   25.00       33.33
## 3        3    1        5    8.33       41.67
## 4        4    2        7   16.67       58.33
## 5        5    1        8    8.33       66.67
## 6        6    1        9    8.33       75.00
## 7        7    1       10    8.33       83.33
## 8        8    1       11    8.33       91.67
## 9        9    1       12    8.33      100.00