简体   繁体   中英

R matrix whose 2nd column contains the squares of the numbers in the 1st column

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM