5

Given a matrix object:

Browse[2]> class(coldists)
[1] "matrix"

That has named rows and columns:

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 ]

How can those columns/rows be accessed by name?

Browse[2]> coldists$estimate
NULL

There is a general question here: why is it difficult to find the attributes of a matrix/dataframe etc? From either RStudio editor or terminal the tab or spacebar do not come up with any suggestions after entering the colname variable name. There is likely a general approach to getting help/variable details here that I am missing. E.g. how can those rownames such as estimate, method, etc be accessed?

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • 1
    `mat[, ]` to access elements, e.g. `coldists["estimate", "diastolic"]`. `dimnames(coldists)` to see the names. – Marius Feb 05 '18 at 00:56
  • Tab completion should work for the column names of a data frame (but not matrix). – neilfws Feb 05 '18 at 00:58
  • @Marius thx but all of the examples you showed are `NULL` – WestCoastProjects Feb 05 '18 at 00:59
  • @neilfws I converted to df: `coldists = as.data.frame(coldists)` : that seems promising but fyi tab completion still does not work. at least now the lookup `coldists["pregnant"] does work. I'd like to find out how to nab the first row "estimate" - probably a filter operation – WestCoastProjects Feb 05 '18 at 01:02

1 Answers1

3

As explained in the comments, but with a working example:

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

m["B", ]
# [1] 2 4 6
AkselA
  • 8,153
  • 2
  • 21
  • 34
  • ouch I see. The matrix *itself* does *not* have these methods. `rownames` is an *independent* method. This is making learning/discovering how to use the `R` constructs more challenging. – WestCoastProjects Feb 05 '18 at 01:07
  • More correct would be to say that row names are an optional attribute of matrices (see `attr(m, "dimnames")`), `rownames()` is just a shortcut for assigning those attributes. The methods of interest are in the `[` function. You can access the relevant documentation through `?'['`. – AkselA Feb 05 '18 at 01:25
  • `R` data structures are not straightforward: many gotchas. E.g. `[[` vs `[` , methods that are standalone vs living on the object itself, more.. – WestCoastProjects Feb 05 '18 at 03:14
  • example in `?matrix` in R 3.5.3. `mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE, dimnames = list(c("row1", "row2"), c("C.1", "C.2", "C.3")))`. Access the column "C.1" as `mdat[,"C.1"]` – Nick Dong Jun 05 '19 at 02:41