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 8522989
In Process

The Archive Base Latest Questions

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

I have the following matrix ‘x’ a b a 1 3 b 2 4

  • 0

I have the following matrix ‘x’

   a b
a 1 3
b 2 4

It is a really large matrix (trimmed down for this question)
I would like to print out this matrix by each row name and column name combination along with the value in that cell. So the expected output would be

a,a,1
a,b,3
b,a,2
b,b,4

I could loop through them, but I’m pretty sure this can be avoided (apply?). Any pointers much appreciated.

  • 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:17:02+00:00Added an answer on June 11, 2026 at 7:17 am

    One way is to use the melt function from the reshape2 package.

    x <- matrix(1:4, nrow = 2, ncol = 2,
                dimnames = list(dim1 = c("a", "b"), dim2 = c("a", "b")))
    
    library(reshape2)
    melt(x)
    #   dim1 dim2 value
    # 1    a    a     1
    # 2    b    a     2
    # 3    a    b     3
    # 4    b    b     4
    

    Edit
    If your data is so big that speed is an issue, I would also suggest:

    data.frame(dim1 = rep(rownames(x), ncol(x)),
               dim2 = rep(colnames(x), each = nrow(x)),
               value = c(x))
    

    Edit2

    After testing with relatively big data, I would not rule out melt:

    x <- matrix(runif(9e6), nrow = 3000, ncol = 3000,
                dimnames = list(dim1 = paste0("x", runif(3000)),
                                dim2 = paste0("y", runif(3000))))
    
    system.time(y1 <- melt(x))
    # user  system elapsed 
    # 1.17    0.44    1.61 
    
    system.time(y2 <- data.frame(dim1 = rep(rownames(x), ncol(x)),
                                 dim2 = rep(colnames(x), each = nrow(x)),
                                 value = c(x)))
    # user  system elapsed 
    # 1.98    0.37    2.36 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following numpy matrix that I want to sort in ascending order
I have data like the following matrix: A = [2 5 10 4 10;
I have a matrix of m.n images like the following: images = zeros( m,
suppose I have the following matrix a = 2 NaN NaN 4 NaN 3
Say I have the following matrix: A = randi(10, [6 3]) 7 10 3
In R I have the following matrix (each row represents a bootstrap 95% confidence
Say I have the following similarity matrix : matrix = [[100.0, 66.666666666666671, 61.539999999999999, 59.260000000000005,
I have the following matrices: m = matrix(c(1:12), nrow=4) p = matrix(c(2,7,11), nrow=1) Per
I have the following code to convert the type of the matrix using cv::Mat.convertTo()
I have a string of style transform given in the following way : matrix(0.312321,

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.