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

  • Home
  • SEARCH
  • 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 7707757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:26:29+00:00 2026-06-01T00:26:29+00:00

I have a matrix in R. Each entry i , j is a score

  • 0

I have a matrix in R. Each entry i,j is a score and the rownames and colnames are ids.

Instead of the matrix I just want a 3 column matrix that has: i,j,score

Right now I’m using nested for loops. Like:

for(i in rownames(g))
    {
        print(which(rownames(g)==i))
        for(j in colnames(g))
        {
            cur.vector<-c(cur.ref, i, j, g[rownames(g) %in% i,colnames(g) %in% j])
            rbind(new.file,cur.vector)->new.file
        }

    }

But thats very inefficient I think…I’m sure there’s a better way I’m just not good enough with R yet.
Thoughts?

  • 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-01T00:26:31+00:00Added an answer on June 1, 2026 at 12:26 am

    If I understand you correctly, you need to flatten the matrix.
    You can use as.vector and rep to add the id columns e.g. :

    m = cbind(c(1,2,3),c(4,5,6),c(7,8,9))
    row.names(m) = c('R1','R2','R3')
    colnames(m) = c('C1','C2','C3')
    
    d <- data.frame(i=rep(row.names(m),ncol(m)),
                    j=rep(colnames(m),each=nrow(m)),
                    score=as.vector(m))
    

    Result:

    > m
       C1 C2 C3
    R1  1  4  7
    R2  2  5  8
    R3  3  6  9
    
    > d
       i  j score
    1 R1 C1     1
    2 R2 C1     2
    3 R3 C1     3
    4 R1 C2     4
    5 R2 C2     5
    6 R3 C2     6
    7 R1 C3     7
    8 R2 C3     8
    9 R3 C3     9
    

    Please, note that this code converts a matrix into a data.frame, since the row and col names can be string and you can’t have a matrix with different column type.
    If you are sure that all row and col names are numbers, you can coerced it to a matrix.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several blocks of the following code that each use there own matrix.
Have a matrix report now that has Position, Hours and Wages for a location
just a quick question, if I have a matrix has n rows and m
I have a matrix with each column represents a feature over time. I need
Context I have a matrix comprising of a sequence of column vectors. Each column
Basically I have 14800x8 matrix that has been extracted from matlab as CSV file
I have a matrix that has 20 rows and 51 columns. I would like
We have an application that stores a sparse matrix. This matrix has entries that
I have a 1000x1000 matrix, with some data in each entry. Is it possible
Suppose I have a matrix such that each row is a standard basis vector,

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.