# Weighted Frequency Table in R (Example)

On this page, I’ll explain how to construct a table with weights in the R programming language.

The content of the post looks as follows:

Here’s the step-by-step process:

## Creating Example Data

The following data will be used as basement for this R tutorial:

```data <- data.frame(values = c(letters[1:3], letters[2:5], "b"),  # Create example data
weights = c(1, 2, 1, 5, 3, 1, 2, 3))
data                                                             # Print example data``` Table 1 shows that our example data has eight rows and two columns. The first variable contains the values that we want to show in a frequency table, and the second column contains the corresponding weights.

## Example: Create Weighted Frequency Table Using wtd.table() Function of questionr Package

The following R programming syntax explains how to create a table with weighting using the R programming language.

For this task, we can use the questionr package. If we want to use the functions of the questionr package, we first have to install and load questionr:

```install.packages("questionr")                                    # Install questionr package

Next, we can apply the wtd.table function of the questionr package to make a weighted frequency table:

```my_wtd_table <- wtd.table(x = data\$values,                       # Create weighted table
weights = data\$weights)
my_wtd_table                                                     # Print weighted table
# a  b  c  d  e
# 1 10  4  1  2```

The previous RStudio console output shows our input data in a weighted frequency distribution table.

## Video & Further Resources

Have a look at the following video on my YouTube channel. In the video tutorial, I show the contents of this tutorial:

Furthermore, you might want to have a look at the related articles on this website.

Subscribe to the Statistics Globe Newsletter

• Eva Alblas
September 15, 2023 8:32 am

Dear Joachim,

Thank you very much for your clear examples, they help me a lot. I have a question about this post (Create Weighted Frequency Table Using wtd.table() Function of questionr Package). I succeeded 😉 However, I would like to make a frequency table of several variables, all using the same weight factor. Is this possible or should I continue with one variable at the time?

• September 15, 2023 9:26 am

Hello Eva,