Create Multiplication Table in R (Example)
In this R tutorial you’ll learn how to construct a multiplication table.
The content of the page looks like this:
It’s time to dive into the programming part!
Example: Create Multiplication Table Using a Manually Defined Function
In this example, I’ll demonstrate how to compute and create a multiplication table in the R programming language.
For this task, we first have to create a user-defined function:
fun_multtab <- function(x, len) { # Create function for multiplication table for(i in 1:len) { print(paste(x, "*", i, "=", x * i)) } }
Next, we can apply this manually defined function to create a multiplication table for a certain input value with a certain length.
The example below constructs a multiplication table for the value 5 up to the multiplier 10:
fun_multtab(x = 5, len = 10) # Apply user-defined function # [1] "5 * 1 = 5" # [1] "5 * 2 = 10" # [1] "5 * 3 = 15" # [1] "5 * 4 = 20" # [1] "5 * 5 = 25" # [1] "5 * 6 = 30" # [1] "5 * 7 = 35" # [1] "5 * 8 = 40" # [1] "5 * 9 = 45" # [1] "5 * 10 = 50"
We can insert basically every input we want to our function. Let’s execute it once again with the values 3 and 7:
fun_multtab(x = 3, len = 7) # Different input values # [1] "3 * 1 = 3" # [1] "3 * 2 = 6" # [1] "3 * 3 = 9" # [1] "3 * 4 = 12" # [1] "3 * 5 = 15" # [1] "3 * 6 = 18" # [1] "3 * 7 = 21"
Looks good!
Video & Further Resources
Do you need further details on the R programming syntax of this article? Then you may watch the following video on my YouTube channel. In the video, I demonstrate the examples of this tutorial in RStudio.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might have a look at some other tutorials on my homepage. Some related articles that are related to the construction of a multiplication table are listed below:
- How to Create a Pivot Table in R
- Proportions with dplyr Package in R
- How to Create a Frequency Table in R
- All R Programming Tutorials
You have learned in this article how to calculate a multiplication table in the R programming language. Please let me know in the comments section below, in case you have additional comments or questions.
Statistics Globe Newsletter