Search code examples
rfrequency-distribution

Frequency Distribution Table


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)

Solution

  • I don't know your exact application but it seems unnecessary to show the data multiple times for each repeated x value. If this is not needed, you can avoid the merge

    x <- c(1,2,3,2,4,2,5,4,6,7,8,9)
    Freq <- table(x)
    relFreq <- prop.table(Freq)
    Cumulative_Freq <- cumsum(Freq)
    Cumulative_Relative_Freq <- cumsum(relFreq)
    data.frame(xval = names(Freq), Freq=Freq, relFreq=relFreq, 
                    Cumulative_Freq=Cumulative_Freq, 
                    Cumulative_Relative_Freq=Cumulative_Relative_Freq)
    

    Another way to accomplish the same thing:

    require(plyr)
    x <- c(1,2,3,2,4,2,5,4,6,7,8,9)
    z <- data.frame(table(x))
    mutate(z, relFreq = prop.table(Freq), Cumulative_Freq = cumsum(Freq), 
                   Cumulative_Relative_Freq = cumsum(relFreq))