通过组合行名称和列名来重塑二维矩阵(data.frame)

wgx48brx  于 2023-07-31  发布在  其他
关注(0)|答案(4)|浏览(106)

提问

我在写论文的时候有个有趣的任务要做。我有一个2D-matrix(或data.frame),如下所示:

CACE        cheng     cheng2        ding    ding_ass        sun2
mean    0 -0.000467158 0.01219119 0.004284223 0.003803375 0.004204354
sd      0  0.131911914 0.14457078 0.074447198 0.055980336 0.072260046
            sun3
mean 0.004202419
sd   0.072266683

字符串
上面的矩阵描述了几个模型的性能(它们的meansd)。我想把它们列到论文中,所以需要这样重新塑造它们:

CACE_mean CACE_sd   cheng_mean  cheng_sd cheng2_mean cheng2_sd
[1,]         0       0 -0.000467158 0.1319119  0.01219119 0.1445708
       ding_mean   ding_sd ding_ass_mean ding_ass_sd   sun2_mean
[1,] 0.004284223 0.0744472   0.003803375  0.05598034 0.004204354
        sun2_sd   sun3_mean    sun3_sd
[1,] 0.07226005 0.004202419 0.07226668


这类似于将matrixdata.frame展平,但不是传统的longwide整形任务。我想知道我们是否可以使用高级函数来实现它。

数据

原始数据(dput):

structure(c(0, 0, -0.000467157971792085, 0.131911914238178, 0.0121911908647192, 
0.144570781843054, 0.00428422254646622, 0.0744471979273107, 0.00380337457776962, 
0.0559803359990803, 0.00420435426517323, 0.0722600458117494, 
0.00420241918783969, 0.0722666828398023), .Dim = c(2L, 7L), .Dimnames = list(
    c("mean", "sd"), c("CACE", "cheng", "cheng2", "ding", "ding_ass", 
    "sun2", "sun3")))

我的尝试

new_names = c(outer(row.names(a),colnames(a),function(x,y){paste(y,x,sep = '_')}))
new_data = t(data.frame(c(a),row.names = new_names))
rownames(new_data) <- NULL

效果很好,但我想知道一些其他的想法

mxg2im7a

mxg2im7a1#

您可以根据模式c oncatenate单元格和setNames

setNames(do.call(c, as.data.frame(dat)),
         paste(rep(colnames(dat), each=2), rownames(dat), sep=".")
)
#   CACE.mean       CACE.sd    cheng.mean      cheng.sd   cheng2.mean     cheng2.sd     ding.mean 
# 0.000000000   0.000000000  -0.000467158   0.131911914   0.012191191   0.144570782   0.004284223 
#     ding.sd ding_ass.mean   ding_ass.sd     sun2.mean       sun2.sd     sun3.mean       sun3.sd 
# 0.074447198   0.003803375   0.055980336   0.004204354   0.072260046   0.004202419   0.072266683

字符串

jckbn6z7

jckbn6z72#

我们可以将矩阵转换为dataframe,将rowname作为单独的列,并将数据转换为更广泛的格式。

df %>%
  as.data.frame() %>%
  tibble::rownames_to_column() %>%
  tidyr::pivot_wider(names_from = rowname, values_from = -rowname)

#   CACE_mean CACE_sd cheng_mean cheng_sd cheng2_mean cheng2_sd ding_mean ding_sd
#      <dbl>   <dbl>      <dbl>    <dbl>       <dbl>     <dbl>     <dbl>   <dbl>
#1         0       0  -0.000467    0.132      0.0122     0.145   0.00428  0.0744
# … with 6 more variables: ding_ass_mean <dbl>, ding_ass_sd <dbl>, sun2_mean <dbl>,
#   sun2_sd <dbl>, sun3_mean <dbl>, sun3_sd <dbl>

字符串

5q4ezhmt

5q4ezhmt3#

和你的方法类似但更紧凑

m <- `colnames<-`(t(c(a)),c(t(outer(colnames(a),paste0("_",rownames(a)),paste0))))

字符串
使得

> m
     CACE_mean CACE_sd   cheng_mean  cheng_sd cheng2_mean cheng2_sd   ding_mean   ding_sd
[1,]         0       0 -0.000467158 0.1319119  0.01219119 0.1445708 0.004284223 0.0744472
     ding_ass_mean ding_ass_sd   sun2_mean    sun2_sd   sun3_mean    sun3_sd
[1,]   0.003803375  0.05598034 0.004204354 0.07226005 0.004202419 0.07226668

m4pnthwp

m4pnthwp4#

我们可以在使用as.data.frame.table转换后使用unite

library(dplyr)
library(tidyr)
as.data.frame.table(m1) %>%
   unite(Var1, Var2, Var1) %>%
   spread(Var1, Freq)
#    CACE_mean CACE_sd   cheng_mean  cheng_sd cheng2_mean cheng2_sd ding_ass_mean ding_ass_sd   ding_mean   ding_sd   sun2_mean    sun2_sd
#1         0       0 -0.000467158 0.1319119  0.01219119 0.1445708   0.003803375  0.05598034 0.004284223 0.0744472 0.004204354 0.07226005
#    sun3_mean    sun3_sd
#1 0.004202419 0.07226668

字符串

相关问题