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.

 

 

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:

 

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.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top