给R中的每两列命名

时间:2018-08-31 12:49:16

标签: r

这是我的矩阵

我想为每两列添加名称。因此,前两列将收到名称“ First Model”,而不会删除下面的名称:EstimatePr(>|t|)。第三和第四列将收到Second Model的名称,第五和第六列将收到名称Third Model

名称EstimatePr(>|t|)不会被删除。

structure(c(0.905134206768795, -1.04453988920406, 0.00745423330005316, 
0.0307781586311053, 3.78116832100487, -6.7629360140366, 4.54368682196926e-07, 
1.01567317751984e-10, -0.330723513150022, 0.906394341970339, 
0.38670651617051, 0.141882775043945), .Dim = c(2L, 6L), .Dimnames = list(
    c("(Intercept)", "complete.ini$idiff_3m.grb[-c(502:504)]"
    ), c("Estimate", "Pr(>|t|)", "Estimate", "Pr(>|t|)", "Estimate", 
    "Pr(>|t|)")))

我该怎么做?

2 个答案:

答案 0 :(得分:1)

也许是这样:

 d <- structure(c(0.905134206768795, -1.04453988920406, 0.00745423330005316, 
                     0.0307781586311053, 3.78116832100487, -6.7629360140366, 4.54368682196926e-07, 
                     1.01567317751984e-10, -0.330723513150022, 0.906394341970339, 
                     0.38670651617051, 0.141882775043945), .Dim = c(2L, 6L), .Dimnames = list(
                       c("(Intercept)", "complete.ini$idiff_3m.grb[-c(502:504)]"
                       ), c("Estimate", "Pr(>|t|)", "Estimate", "Pr(>|t|)", "Estimate", 
                            "Pr(>|t|)")))
 names(dimnames(d)) <- c("","  First Model           Second Model          Third Model")
 d

结果:

                                           First Model           Second Model           Third Model
                                           Estimate    Pr(>|t|)  Estimate     Pr(>|t|)   Estimate  Pr(>|t|)
  (Intercept)                             0.9051342 0.007454233  3.781168 4.543687e-07 -0.3307235 0.3867065
  complete.ini$idiff_3m.grb[-c(502:504)] -1.0445399 0.030778159 -6.762936 1.015673e-10  0.9063943 0.1418828

您只需添加空格即可使标签适合names(...

答案 1 :(得分:0)

像这样吗?

k<-structure(c(0.905134206768795, -1.04453988920406, 0.00745423330005316, 
0.0307781586311053, 3.78116832100487, -6.7629360140366, 4.54368682196926e-07, 
1.01567317751984e-10, -0.330723513150022, 0.906394341970339, 
0.38670651617051, 0.141882775043945), .Dim = c(2L, 6L), .Dimnames = list(
c("(Intercept)", "complete.ini$idiff_3m.grb[-c(502:504)]"
), c("Estimate", "Pr(>|t|)", "Estimate", "Pr(>|t|)", "Estimate", 
"Pr(>|t|)")))

colnames(k)<-c(   
paste0("Model1.",colnames(k)[1]),paste0("Model1.",colnames(k)[2]), 
paste0("Model2.",colnames(k)[1]),paste0("Model2.",colnames(k)[2]),
paste0("Model3.",colnames(k)[1]),paste0("Model3.",colnames(k)[2])
)

k
                                           Model1.Estimate Model1.Pr(>|t|) Model2.Estimate Model2.Pr(>|t|) Model3.Estimate Model3.Pr(>|t|)
(Intercept)                                  0.9051342     0.007454233        3.781168    4.543687e-07      -0.3307235       0.3867065
complete.ini$idiff_3m.grb[-c(502:504)]      -1.0445399     0.030778159       -6.762936    1.015673e-10       0.9063943       0.1418828