What a table in R actually is, and why you'd make one

A table in R is a way to count how many times each value appears in your data. If you have a column of city names — New York, Boston, New York, Chicago, Boston, Boston — R can count them and show you that Boston appears 3 times, New York 2 times, and Chicago 1 time. That count is the table.

You use tables when you want to see patterns in categories: which product sold most often, how many people chose each answer on a survey, or whether one type of error shows up more than another. Tables turn a long list of repeated values into a summary you can actually read.

The most common way to make a table in R is with the table() function. You give it a column of data, and it counts each unique value and shows you the results.

Key Takeaways

  • The table() function counts how many times each value appears in a column and displays the results as a summary.
  • You can create a table from a single column with table(column_name) or from two columns to see how values pair up with table(column1, column2).
  • If your data is in a data frame (a spreadsheet-like structure in R), use the dollar sign to point to the column: table(data_frame$column_name).
  • The output shows each unique value and its count, and you can save the result to a new object to use it later in your analysis.

Creating a table from a single column

Start with the simplest case: you have one column of data and want to count the values. Load your data into R first. If your data is in a CSV file called survey_responses.csv, use read.csv("survey_responses.csv") to bring it in. Store it in an object so you can work with it — for example, my_data <- read.csv("survey_responses.csv").

Now say your data has a column called favorite_color. To see how many people chose each color, type table(my_data$favorite_color). The dollar sign tells R to look inside the my_data object for the column named favorite_color. R will print out something like:

blue green red yellow 12 8 15 5

This means 12 people chose blue, 8 chose green, 15 chose red, and 5 chose yellow. You can save this table to a new object if you want to use it later: color_counts <- table(my_data$favorite_color). Then you can refer to color_counts in other commands.

Creating a table from two columns

Sometimes you want to see how two categories relate to each other. For example, you might have a column for product_type (shoes, socks, hats) and another for size (small, medium, large). A two-column table shows you how many of each product came in each size.

Use the same table() function but list both columns: table(my_data$product_type, my_data$size). R creates a grid where rows are product types and columns are sizes. The numbers in each cell show how many items match that combination. This layout makes it straightforward to spot patterns — for instance, whether hats sell more in large sizes than shoes do.

You can also save a two-column table to an object: product_size_table <- table(my_data$product_type, my_data$size). Later, if you want to see just the row names (the product types), type rownames(product_size_table). For column names (the sizes), use colnames(product_size_table).

Turning a table into a data frame so you can export it

Once you have a table, you might want to save it as a CSV file or use it in another program. Tables in R have a special format that does not export cleanly. The solution is to convert the table to a data frame first using as.data.frame().

If your table is stored in color_counts, type color_df <- as.data.frame(color_counts). This creates a new data frame with two columns: one for the color names and one for the counts. Then export it with write.csv(color_df, "color_counts.csv"). The file will appear in your working directory (the folder R is currently using) and you can open it in Excel or any spreadsheet program.

Sorting a table so the highest counts appear first

By default, R shows table results in the order the values first appeared in your data, not in order of how many times they appeared. If you want to see the most common values first, use the sort() function with the option decreasing = TRUE.

Type sort(table(my_data$favorite_color), decreasing = TRUE). This counts the colors, then sorts them from highest count to lowest. Now you see red (15) first, then blue (12), then green (8), then yellow (5). If you want them sorted lowest to highest instead, use decreasing = FALSE or leave that part out entirely.

Handling missing values in your table

If your data has empty cells or NA values (R's way of marking missing data), the table() function ignores them by default. This is usually what you want — you do not need a count of "missing". But if you want to see how many missing values you have, add useNA = "ifany" to your command.

Type table(my_data$favorite_color, useNA = "ifany"). Now the output includes a row or column labeled NA showing how many cells were empty. If there are no missing values, this option does nothing. If you want to see NA in the output even when there are zero missing values, use useNA = "always" instead.

Using table results in further analysis

Once you have a table, you can do math with it. If you saved your table as color_counts, you can find the total number of responses with sum(color_counts). To find the percentage each color represents, divide each count by the total: color_counts / sum(color_counts). Multiply by 100 to get percentages: (color_counts / sum(color_counts)) * 100.

You can also find the most common value with max(color_counts) (which gives you the highest count) or which.max(color_counts) (which tells you which value has the highest count). These commands let you move from a straightforward count to actual insights about your data.

Frequently Asked Questions

What is the difference between table() and data.frame()?

table() counts values and shows the results as a summary. data.frame() creates a spreadsheet-like structure that holds your original data. They serve different purposes: use table() to summarize, use data.frame() to organize raw data.

Can I make a table from more than two columns?

Yes. Type table(column1, column2, column3) and R will create a multi-dimensional table. The output becomes harder to read as you add more columns, so most people stick to one or two and create separate tables for different combinations instead.

Why does my table show values I did not expect?

Check whether your column contains spaces, capital letters, or typos. "Red", "red", and "Red " are three different values to R. Use unique(my_data$column_name) to see exactly what values are in your column, then clean them up if needed before making the table.

How do I add labels to my table rows and columns?

Use rownames() and colnames() to change them. For example, colnames(my_table) <- c("Small", "Medium", "Large") replaces the column names with the labels you provide in the list.