漂亮地打印字符串的 R 矩阵?
Pretty printing an R matrix of strings?
我有这样的东西:
[,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)"
我想知道是否可以漂亮地打印这个矩阵,例如将每个元素右对齐、省略引号等?
您可以将其转换为 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
我有这样的东西:
[,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)"
我想知道是否可以漂亮地打印这个矩阵,例如将每个元素右对齐、省略引号等?
您可以将其转换为 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