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 data like
> x
[1] 17 17 17 17 17 17 17 17 16 16 16 16 16 18 18 18 10 12 17 17 17 17 17 17 17 17 16 16 16 16 16 18 18 18 10
[36] 12 15 19 20 22 20 19 19 19
a table like:
frequency cumulative relative
(9.99,11.7] 2 2 0.04545455
(11.7,13.4] 2 4 0.04545455
(13.4,15.1] 1 5 0.02272727
(15.1,16.9] 10 15 0.22727273
(16.9,18.6] 22 37 0.50000000
(18.6,20.3] 6 43 0.13636364
(20.3,22] 1 44 0.02272727
I know it should be simple, but I don't know how.
I got some results using this code:
factorx <- factor(cut(x, breaks=nclass.Sturges(x)))
as.matrix(table(factorx))
My suggestion is to check the agricolae package... check it out:
You're close! There are a few functions that will make this easy for you, namely
cumsum()
andprop.table()
. Here's how I'd probably put this together. I make some random data, but the point is the same:If you are looking for something pre-packaged, consider the
freq()
function from thedescr
package.Or to get cumulative percents, use the
ordered()
functionTo add a "cumulative frequencies" column:
If your data has missing values, a valid percent column is added to the table.
The base functions
table
,cumsum
andprop.table
should get you there: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:
Yet another possibility: