如何截断 R 矩阵列名仅用于可视化,同时保留基础完整列名以供处理?

How to truncate R matrix column names for visualization only while leaving the underlying complete column names intact for processing?

提问人:Village.Idyot 提问时间:11/1/2023 更新时间:11/1/2023 访问量:44

问:

我正在使用多个交互式矩阵,这些矩阵被合并到一个列表中,用于组织和闪亮渲染目的。由于正在生成的矩阵数和矩阵列数,唯一的矩阵列名称变得非常长,使矩阵难以在 R Studio 中查看。我需要查看由函数生成的临时矩阵以进行测试。

R pro 如何处理此类问题,仅出于可视化目的截断矩阵列名,同时保留长列名不变以进行处理?或者,R pro 是否创建矩阵名称和列名称的键索引,并分配矩阵名称(如 mat1、mat2 等)和列名称 mat1Col1、mat1Col2 等?后者对我来说似乎很困难,矩阵列名即使很长也更容易不言自明。

下面发布的代码是编辑。实际代码中的矩阵列名称比此示例中显示的名称长得多。您可以看到矩阵被嵌入到列表中,提取了长矩阵列名称的一部分并将其插入到列表名称中,以便进行嵌入式矩阵标识。运行输出矩阵的函数将生成下图的上半部分,运行输出带有嵌入矩阵的列表的函数将生成下图的下半部分:

enter image description here

法典:

create_Tmp1 <- function(element, variable) {

  col_1 <- paste('Begin_testing_element', element, sep = "_")
  col_2 <- paste('Run_tests_element', element, sep = "_")
  col_3 <- paste('End_testing_element', element, sep = "_")
  
  matTmp1 <- matrix(0, nrow = 3, ncol = 3)
  colnames(matTmp1) <- c(col_1,col_2,col_3)
  
  
  # For-loop to generate running table
  for(i in seq(nrow(matTmp1))){
    if(i > 1){matTmp1[i,col_1] = matTmp1[i-1,col_3]}
    matTmp1[i,col_2] = variable
    matTmp1[i,col_3] = matTmp1[i,col_1] + matTmp1[i,col_2]
  }
  
  matTmp1_to_list <- matTmp1
  new_colnames <- sub("_element.*$", "", colnames(matTmp1))
  colnames(matTmp1_to_list) <- new_colnames
  
  listTmp1 <- list(matTmp1 = list())
  listTmp1$matTmp1[[element]] <- matTmp1_to_list
  
  return(listTmp1)
  # return(matTmp1)
}

create_Tmp1("once_upon_a_time",5)
R 矩阵

评论

1赞 jay.sf 11/1/2023
或。?abbreviate

答:

0赞 Wimpel 11/1/2023 #1

也许您可以使用标签(据我所知,仅适用于 data.frames)。

# sample data
mymatrix <- matrix(rep(1:3, 3), ncol = 3)
colnames(mymatrix) <- paste0("My_Long_Colname", 1:3)


library(sjlabelled)
library(labelled)
# labels only wotk on vectors or data.frames, 
#  so create a data.frame from the mar
mydf <- as.data.frame(mymatrix)
# set labels to each colum (here: a,b,c are used)
mydf <- labelled::set_variable_labels(mydf, .labels = letters[1:3])
# print with labels as colnames
sjlabelled::label_to_colnames(mydf)
#   a b c
# 1 1 1 1
# 2 2 2 2
# 3 3 3 3

# proof that the actual conames are still intact:
mydf
#   My_Long_Colname1 My_Long_Colname2 My_Long_Colname3
# 1                1                1                1
# 2                2                2                2
# 3                3                3                3
1赞 Rui Barradas 11/1/2023 #2

您可以为 class 的对象编写方法。此方法可以缩写列名,然后调用通常的 ."matrix"print.default

print.matrix <- function(x, ..., size = 4L) {
  cn <- colnames(x)
  if(any(nchar(cn) > size)) {
    cn <- abbreviate(cn, minlength = size, strict = TRUE, method = "both")
    colnames(x) <- cn
  }
  NextMethod()
}

m <- matrix(1:12, 3, dimnames = list(NULL, state.name[1:4]))

print.default(m)
#>      Alabama Alaska Arizona Arkansas
#> [1,]       1      4       7       10
#> [2,]       2      5       8       11
#> [3,]       3      6       9       12

# default is size = 4L
print(m)
#>      Albm Alsk Arzn Arkn
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12

# size is too small, printed names are equal,
# original names do not change
print(m, size = 2L)
#>      Al Al Ar Ar
#> [1,]  1  4  7 10
#> [2,]  2  5  8 11
#> [3,]  3  6  9 12

m
#>      Albm Alsk Arzn Arkn
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12

创建于 2023-11-01 with reprex v2.0.2