How to generate a frequency table in R with with cumulative frequency and relative frequency
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 datax <- sample(10:20, 44, TRUE)#Your codefactorx <- factor(cut(x, breaks=nclass.Sturges(x)))#Tabulate and turn into data.framexout <- as.data.frame(table(factorx))#Add cumFreq and proportionsxout <- transform(xout, cumFreq = cumsum(Freq), relative = prop.table(Freq))#----- factorx Freq cumFreq relative1 (9.99,11.4] 11 11 0.250000002 (11.4,12.9] 3 14 0.068181823 (12.9,14.3] 11 25 0.250000004 (14.3,15.7] 2 27 0.045454555 (15.7,17.1] 6 33 0.136363646 (17.1,18.6] 3 36 0.068181827 (18.6,20] 8 44 0.18181818
The base functions table
, cumsum
and prop.table
should get you there:
cbind( Freq=table(x), Cumul=cumsum(table(x)), relative=prop.table(table(x))) Freq Cumul relative10 2 2 0.0454545512 2 4 0.0454545515 1 5 0.0227272716 10 15 0.2272727317 16 31 0.3636363618 6 37 0.1363636419 4 41 0.0909090920 2 43 0.0454545522 1 44 0.02272727
With cbind and naming of the columns to your liking this should be pretty easy for you in the future. The output from the table function is a matrix, so this result is also a matrix. If this were being done on something big it would be more efficient todo this:
tbl <- table(x)cbind( Freq=tbl, Cumul=cumsum(tbl), relative=prop.table(tbl))
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)