Statistics for Sociology/Presenting Data

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Creating a Frequency Distribution in R[edit]

It's quite useful to create a frequency for many variables, particularly those that are nominal and ordinal, to get a sense of how many cases fall into each category. In R, creating a frequency distribution can be done with the following command:

table(dataset$variable)

If you want to contrast two variables, you can use the same command, but with two variables:

table(dataset$variable1, variable2)

If you want to convert the table into proportions, you would need to first save the table as a variable then use a different command:

frequencytable <- table(dataset$variable1, variable2)
prop.table(frequencytable, 1)

NOTE: By default, R excludes missing values in tables. If you want missing values include, the following option needs to be included in the command "exclude = NULL". So, it would look like this:

table(dataset$variable, exclude = NULL)