Home > Software engineering >  R matrix whose 2nd column contains the squares of the numbers in the 1st column
R matrix whose 2nd column contains the squares of the numbers in the 1st column

Time:11-25

I am new to R. I need to make a matrix in R whose 1st column contains the numbers 6,7,8,9 and whose 2nd column contains the squares of the numbers in the 1st column.

CodePudding user response:

There are many possible solutions to your problem. One way is to use cbind(), e.g.

mat <- matrix(6:9)
mat
#>      [,1]
#> [1,]    6
#> [2,]    7
#> [3,]    8
#> [4,]    9
mat2 <- cbind(mat, mat^2)
mat2
#>      [,1] [,2]
#> [1,]    6   36
#> [2,]    7   49
#> [3,]    8   64
#> [4,]    9   81
  • Related