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 |
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 library("questionr") # Load questionr |
install.packages("questionr") # Install questionr package library("questionr") # Load questionr
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 |
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:
The YouTube video will be added soon.
Furthermore, you might want to have a look at the related articles on this website.
- Proportions with dplyr Package in R
- Cumulative Frequency & Probability Table in R
- All R Programming Examples
In this article, I have demonstrated how to create a weighted frequency table in R programming. Tell me about it in the comments, in case you have additional questions. In addition, please subscribe to my email newsletter to get updates on new tutorials.
Statistics Globe Newsletter