如何将向量转换为R中的矩阵? [英] How to turn a vector into a matrix in R?

查看:1273
本文介绍了如何将向量转换为R中的矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有49个数值的向量.我想改用7x7的数字矩阵.

I have a vector with 49 numeric values. I want to have a 7x7 numeric matrix instead.

是否可以使用某种方便的自动转换语句,还是必须将7个正确的向量子集的单独列分配给新矩阵?我希望有类似c(myMatrix)的东西,当然可以选择给出我想要的行数和/或列数.

Is there some sort of convenient automatic conversion statement I can use, or do I have to do 7 separate column assignments of the correct vector subsets to a new matrix? I hope that there is something like the oposite of c(myMatrix), with the option of giving the number of rows and/or columns I want to have, of course.

推荐答案

只需使用matrix:

matrix(vec,nrow = 7,ncol = 7)

使用matrix而不是像Gavin指出的那样简单地更改维度属性的一个优点是,您可以使用matrix中的byrow参数指定矩阵是按行还是按列填充.

One advantage of using matrix rather than simply altering the dimension attribute as Gavin points out, is that you can specify whether the matrix is filled by row or column using the byrow argument in matrix.

这篇关于如何将向量转换为R中的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆