Frequency Tables
In what follows, it is assumed that data are available as a data.frame; in other words, it assumed that there columns of data with "names" attached. Some other classes may also work.
Frequency Tables
As yet, I have not found a way to create a simple frequency tables (possibly with absolute, relative and cumulative frequencies) in a user friendly way. I'm afraid that's the way R is!
For a table containing all three elements mentioned in the previous paragraph try the following series of commands:
tab1a <- table(name-of-file$variablename)
tab1r <- prop.table(tab1a)
tab1c <- cumsum(tab1r)
tab1full <- cbind(tab1a, tab1r,tab1c)
Note how each line builds on its predecessor: The relative frequencies are built from the table of absolute frequencies, and the cumulative frequencies from the relative frequencies. The final line binds all three objects together into a single object that resembles the frequency table we've grown accustomed to.
Elementary Plots
A plot representing frequencies by thin (vertical) lines is produced by
plot(tab1a)
Alternatively, you may use
barplot(tab1a)
A somewhat longer version of this command might go like this:
barplot(tab1a, ylim=c(0,300), xlab="Status",
names.arg=c("Single", "Married", "Divorced", "Widow/er"), col="lightblue")
Here, xlim
ensures that the scale of the y axis ranges from 0 to 300, xlab
will add a label to the x axis, names.arg
provides labels for the bars (e.g., if the table has no labels, or if you are not pleased by those that there are), and col
controls the color of the bar (which by default is grey). Obviously, this does not exhaust the possibilities.
© W. Ludwig-Mayerhofer, IGRW | Last update: 30 Jan 2017