Tables with R

The istat data set contains gender, geographical area, weight and height for 1806 people. The frequency table for the gender or the geographical area can be obtained using the table() function.

A contingency table of the gender and geographical area can be built.

The airquality data set contains daily air quality measurements in New York from May to September 1973. How many days the Ozone measurements was greater than 180 ppb for each month? Ozone measurements contains NA values.

The above table doesn’t show missing values. The useNA argument allows to manage NA values. Its default value is “no”. “ifany” shows missing values when present. “always” shows a NA level also when there are not missing valuse.

To get relative frequencies, the prop.table() function can be used. Its input are: a table, returned from table() function, and the index to generate margin for.

The margin.table() function returns the sum by row or by column of values of the table.

Margins on a table can be added using the addmargins() function.

addmargins() allows to add any function. The code below adds the mean.