如何按名称从 R 矩阵访问行和列

jav*_*dba 5 r matrix

给定一个matrix对象:

Browse[2]> class(coldists)
[1] "matrix"
Run Code Online (Sandbox Code Playgroud)

已命名行和列:

Browse[2]> coldists
            pregnant    glucose     diastolic   skin        insulin     bmi         pedigree    age        
estimate    Numeric,2   Numeric,2   Numeric,2   Numeric,2   Numeric,2   Numeric,2   Numeric,2   Numeric,2  
method      "mle"       "mle"       "mle"       "mle"       "mle"       "mle"       "mle"       "mle"      
sd          Numeric,2   Numeric,2   Numeric,2   Numeric,2   Numeric,2   Numeric,2   Numeric,2   Numeric,2  
cor         Numeric,4   Numeric,4   Numeric,4   Numeric,4   Numeric,4   Numeric,4   Numeric,4   Numeric,4  
vcov        Numeric,4   Numeric,4   Numeric,4   Numeric,4   Numeric,4   Numeric,4   Numeric,4   Numeric,4  
loglik      -2022.201   -3750.272   -3364.823   -3216.296   -4734.98    -2675.054   -240.8774   -2982.152  
 [ reached getOption("max.print") -- omitted 11 rows ]
Run Code Online (Sandbox Code Playgroud)

如何通过名称访问这些列/行?

Browse[2]> coldists$estimate
NULL
Run Code Online (Sandbox Code Playgroud)

这里有一个普遍的问题:为什么很难找到矩阵/数据框等的属性?从RStudio编辑器或终端tab输入变量名称后,空格键或空格键不会出现任何建议colname。我可能缺少一种获取帮助/变量详细信息的通用方法。rownames例如estimate,如何method访问 、 等?

Aks*_*elA 3

正如评论中所解释的,但有一个工作示例:

m <- matrix(1:6, 2)
rownames(m) <- c("A", "B")

m["B", ]
# [1] 2 4 6
Run Code Online (Sandbox Code Playgroud)