How to create a matrix with random values in R? - GeeksforGeeks

16 May, 2021
In this article, we will discuss hw to create a matrix with random values in the R programming language.
The functions which are used to generate the random values are:
rnorm()
runif()
rexp()
rpois()
rbinom()
sample()
We will use all these functions one by one to create the matrix with random values.
Method 1: Using rnorm()
rnorm() function basically creates the random values according to the normal distribution. 
Syntax: rnorm(n, mean, sd)
So, we give 25 as an argument in the rnorm() function, after that put those values in the matrix function with the row number and create the matrix.
R
# matrix create with the help# of matrix function random values# generated with the help of rnorm()m<-matrix(rnorm(25) , nrow = 5)  # print the matrixprint(m)
Output:
Method 2: Using runif() function
runif() function basically creates the random values according to the uniform distribution. So, we give 25 as an argument in the runif() function.
Syntax: runif(n, min, max)
Parameters:n: represents number of observationsmin, max: represents lower and upper limits of the distribution
Code:
R
# matrix create with the help # of matrix function random values # generated with the help of runif()m <- matrix( ruif(25), nrow = 5)  # print the matrixprint(m)
Output:
Method 3: Using rexp() function
rexp() function basically creates the random values according to the exponential distribution. So, we give 25 as an argument in the rexp() function.
Syntax: rexp(N, rate )
Code:
R
# matrix create with the help # of matrix function random values# generated with the help of runif()m <- matrix( runif(25), nrow = 5)  # print the matrixprint(m)
Output:
Method 4: Using rpois() function
 In this example, we will try to create the random values using the rpois(). rpois() function basically creates the random values according to the Poisson distribution x ~ P(lambda). So, we give 25 and 5 as an argument in the rpois() function.
Syntax: rpois(N, lambda)
Parameters:N: Sample Sizelambda: Average number of events per interval
Code:
R
# matrix create with the help # of matrix function random values # generated with the help of rpois()m <- matrix(rpois( 25, 5), nrow = 5)  # print the matrixprint(m)
Output:
Method 5: Using rbinom() function
In this example, we will try to create the random values using the rbinom(). rbinom() function basically creates the random values of a given probability. 
rbinom(n, N, p)
Where n is the number of observations, N is the total number of trials, p is the probability of success. So, we give 25, 5, and .6 as an argument in the rbinom() function.
Code:
R
# matrix create with the help# of matrix function random values# generated with the help of rbinom()m <- matrix(rbinom( 25, 5, .6), nrow = 5)  # print the matrixprint(m)
Output:
Method 6: Using sample() function
In this example, we will try to create random values using the sample(). sample() function basically creates the random values of given elements.
Syntax:sample(x, size, replace)
Parameters:x: indicates either vector or a positive integer or data framesize: indicates size of sample to be takenreplace: indicates logical value. If TRUE, sample may have more than one same value
So, we give 1:20 and 100 as an argument in the sample() function.
Code:
R
# matrix create with the help# of matrix function random values# generated with the help of sample()m <- matrix(sample(  1 : 20, 100, replace = TRUE), ncol = 10)  # print the matrixprint(m)
Output:
Picked
R Matrix-Programs
R-Matrix
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R