How to run a cross table in R


Running a cross table, or cross tab, in R is useful for a number of applications for data research. In the example below, we run a cross tab in R to demonstrate how to find the number, or count, of weather types in a specific city. Essentially we are tallying the data.

This is going to be a really simple example of running cross tabs in R. You could imagine pulling in large volumes of data and running a cross tab to quickly derive information before diving deeper. The cross tabs count could be an indicator of things you want to analyze further or not.

Below we will build a data frame with two sets of data:

> weatherType <- c("rain","snow","rain","snow","sun","snow","sun","snow", "sun");
> location <- c("NYC", "Chicago", "Miami", "NYC", "Chicago", "Miami","NYC", "Chicago", "Miami")
> forecast <- data.frame(weatherType, location) # build a dataframe
> forecast.crosstab <- table(forecast$weatherType, forecast$location); forecast.crosstab # build the cross tab and set up the rows and columns
       Chicago Miami NYC
  rain       0     1   1
  snow       2     1   1
  sun        1     1   1

Yes, we know it shouldn't be snowing in Miami, but this could indicate an issue with your data before you waste time running a large scale data analysis project. If for example you were studying the effects of sunlight on the population, and had a list of all towns and cities in America, a cross tab could quickly show you where to focus your attention.


Please enter your comment!
Please enter your name here