漂亮打印R字符串矩阵?

时间:2015-04-06 17:24:31

标签: r pretty-print

我有这样的事情:

     [,1]                   [,2]                  [,3]
[1,] "75 (0.6, 102.549)"    "6 (23.655, 103.078)" "12 (-33.897, 85.281)"
[2,] "9 (52.26, 46.831)"    "2 (79.672, 66.679)"  "NA (NA, NA)"
[3,] "5 (-38.776, 125.591)" "NA (NA, NA)"         "4 (-25.222, 112.685)"
[4,] "1 (-206.245, NA)"     "NA (NA, NA)"         "NA (NA, NA)"

我想知道是否可以打印这个矩阵,例如将每个元素对齐,省略引号等?

1 个答案:

答案 0 :(得分:4)

您可以将其转换为data.frame,其默认打印方法几乎就是您所要求的:

## Easily-reproducible example matrix
m <- matrix(replicate(12, paste0(sample(letters, sample(1:10)), collapse="")), 
            ncol=3)

## Print it like a data.frame
data.frame(m)
#          X1         X2         X3
# 1 liqfodhkp    lbvxucd gpizqtxcjr
# 2 rfeabvshl   egkzjcyr  yuqzbpocd
# 3     hfuja wlyepibndq    jtnrpsz
# 4        ei        meq      fbwlg