Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8524501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:42:15+00:00 2026-06-11T07:42:15+00:00

When dimnames is currently NULL , is it possible to re-name a matrix’s dimestions

  • 0

When dimnames is currently NULL, is it possible to re-name a matrix’s dimestions one at a time?

For example, this fails:

mtx <- matrix(1:16,4)
dimnames(mtx)[[2]][1] <- 'col1'

with Error in dimnames(mtx)[[2]][1] <- "col1" : 'dimnames' must be a list

However this works:

mtx <- matrix(1:16,4)
dimnames(mtx)[[1]] <- letters[1:4]
dimnames(mtx)[[2]] <- LETTERS[1:4]
dimnames(mtx)[[2]][1] <- 'col1'
dimnames(mtx)[[2]][2] <- 'col2'

My objective is to seperately replace dimnames(mtx)[[2]][1] and dimnames(mtx)[[2]][2] etc … if this is not possible, i can re-write the loop.


Thanks folks, I have ended up with the below — I pass the names in via prepend:

mtxNameSticker <- function(mtx, prepend = NULL, MARGIN=2)
{
    if (MARGIN == 1) max <- nrow(mtx) else
        max  <- ncol(mtx)
    if (is.null(prepend)) if (MARGIN == 2) prepend <- 'C' else
        prepend <- 'R'
    if (length(prepend) == 1) prepend <- paste0(prepend, 1:dim(mtx)[[MARGIN]]) 
    dimnames(mtx)[[MARGIN]] <- seq(from=1, by=1, length.out=dim(mtx)[[MARGIN]]) 
    for (i in 1:max){
        dimnames(mtx)[[MARGIN]][i] <- prepend[i]
    }
    return(mtx)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:42:16+00:00Added an answer on June 11, 2026 at 7:42 am

    For as long as dimnames is NULL and not an appropriate list, you cannot make assignments to it at particular positions. One easy way to create a dummy but complete list of dimnames is to run:

    dimnames(mtx) <- lapply(dim(mtx), seq_len)
    mtx
    #   1 2  3  4
    # 1 1 5  9 13
    # 2 2 6 10 14
    # 3 3 7 11 15
    # 4 4 8 12 16
    

    Then, you can make assignments one at a time like you were wishing:

    dimnames(mtx)[[2]][1] <- 'col1'
    mtx
    #   col1 2  3  4
    # 1    1 5  9 13
    # 2    2 6 10 14
    # 3    3 7 11 15
    # 4    4 8 12 16
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

check this example: > a = matrix(1:9, nrow = 3, ncol = 3, dimnames
This is my first time using a 3 dimensional array and I am having
Please, put this data structure into R in order to reproduce my example: dX
Very simple concept: data(iris) d1=as.matrix(iris) d2=as.array(iris) Error in <code>dimnames<-.data.framecode>(<code>*tmp*code>, value = list(n)) :invalid 'dimnames'
I have an xts object, the first column of which is date-time, followed by
I want to call managed code if and only if the currently executing assembly
Having just spent an hour chasing a 'matrix vs data.frame' bug in my code,
I have some code that I have inherited that generates a matrix of significance
I have a list like this list <- structure(list(`1` = structure(c(274L, 173L), .Dim =
I have a logical matrix x with named rows ('a' and 'b') and named

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.