如何将矩阵的colnames设置为另一个矩阵的colnames?

时间:2014-09-02 19:25:39

标签: r matrix multiple-columns

我有一个名为as.mat的数据矩阵,其维度为[1] 79 1929,另一个矩阵dd.mat的维度为[1] 79 1924dd.mat有1924列,但第1-1921列没有名称。 例如,这就是我colnames(dd.mat)时的样子:

[1] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" [28] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

......直到1921年的专栏。但是列1922,1923和1924的列名为“nv”:

[1918] "" "" "" "" "nv" "nv" "nv"

如何在不重命名第1922行到第1924列的情况下,将as.mat的列号设置为矩阵dd.mat的前1921列名称?

1 个答案:

答案 0 :(得分:2)

只需将作业与索引结合起来

colnames(as.mat)[1:1921] <- colnames(dd.mat)[1:1921]