# How to create a matrix with only one row in R?

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

To create a matrix with only one row in R, we can set the nrow argument of matrix function to 1.

For example, if we want to create a matrix say M with only one row then it can be done by using the command given below −

M<-matrix(1:5,nrow=1)

Check out the below examples to understand the output of one row matrix.

## Example 1

To create a matrix with only one row in R, use the code given below −

M1<-matrix(rpois(5,1),nrow=1)
M1

The following matrix is created −

   [,1] [,2] [,3] [,4] [,5]
[1,] 0   2    1    1    1

## Example 2

To create a matrix with only one row in R, use the code given below −

M2<-matrix(rpois(5,5),nrow=1)
M2

The following matrix is created −

   [,1] [,2] [,3] [,4] [,5]
[1,] 2   4    6    8    2

## Example 3

To create a matrix with only one row in R, use the code given below −

M3<-matrix(rpois(10,5),nrow=1)
M3

The following matrix is created −

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 8   7    3    2     5    5   6    6     4   3

## Example 4

To create a matrix with only one row in R, use the code given below −

M4<-matrix(rpois(2,5),nrow=1)
M4

The following matrix is created −

   [,1] [,2]
[1,] 2   3

## Example 5

To create a matrix with only one row in R, use the code given below −

M5<-matrix(rpois(2,50),nrow=1)
M5

The following matrix is created −

   [,1] [,2]
[1,] 37  38

## Example 6

To create a matrix with only one row in R, use the code given below −

M6<-matrix(rpois(10,50),nrow=1)
M6

The following matrix is created −

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 62   49   46   53   49   58   50   48   49   53

## Example 7

To create a matrix with only one row in R, use the code given below −

M7<-matrix(rnorm(6),nrow=1)
M7

The following matrix is created −

        [,1]    [,2]      [,3]     [,4]       [,5]     [,6]
[1,] 0.9545686 0.5495047 1.403797 -0.1845401 0.5164328 -0.9767084

## Example 8

To create a matrix with only one row in R, use the code given below −

M8<-matrix(rnorm(6,10,2),nrow=1)
M8

The following matrix is created −


[,1]   [,2]     [,3]     [,4]    [,5]    [,6]
[1,] 10.35974 7.565574 9.575034 13.29151 6.77356 9.180579

## Example 9

To create a matrix with only one row in R, use the code given below −

M9<-matrix(runif(3,2,10),nrow=1)
M9

The following matrix is created −

       [,1]      [,2]    [,3]
[1,] 4.652802 2.331193 9.452688

## Example 10

To create a matrix with only one row in R, use the code given below −

M10<-matrix(rexp(4),nrow=1)
M10

The following matrix is created −

       [,1]     [,2]      [,3]     [,4]
[1,] 0.9369164 4.111468 1.368531 1.036329

## Example 11

To create a matrix with only one row in R, use the code given below −

M11<-matrix(round(rnorm(5),2),nrow=1)
M11

The following matrix is created −

      [,1] [,2] [,3]  [,4]  [,5]
[1,] 0.25  2.8 -0.49 -0.68 -0.1

## Example 12

To create a matrix with only one row in R, use the code given below −

M12<-matrix(round(runif(3,2,10),2),nrow=1)
M12

The following matrix is created −

     [,1] [,2] [,3]
[1,] 4.75 6.62 6.56
Updated on 09-Nov-2021 07:36:31