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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:59:41+00:00 2026-06-15T10:59:41+00:00

I have a question on how to fill in a matrix by its column

  • 0

I have a question on how to fill in a matrix by its column names. I think the following example would make it clear what I want.

mat <- matrix(NA, nrow = 10, ncol = 5)
colnames(mat) <- c("Apple", "Orange", "Pear", "Grape", "Mango")

Now I have a new matrix whose row names are a subset of the column names in mat, and with an arbitrary order.

jmat <- matrix(rnorm(4), nrow = 4, ncol = 1)
rownames(jmat) <- sample(c("Apple", "Orange", "Grape", "Mango"))

I want to fill the first row of mat by the corresponding values in jmat. The value of “Apple” in jmat should be in the “Apple” column in mat, etc. Because there is no “Pear” row in jmat, so the “Pear” column in mat will still be a NA. What is the simplest way of doing that?

Thank you.

  • 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-15T10:59:43+00:00Added an answer on June 15, 2026 at 10:59 am

    I found that match function can give the indices of a vector within another vector.

    > jmat
                 [,1]
    Grape  -0.2728387
    Apple   2.2843550
    Mango  -1.3079324
    Orange -0.6129420
    > match(rownames(jmat), colnames(mat))
    [1] 4 1 5 2
    > ind <- match(rownames(jmat), colnames(mat))
    > mat[1, ind] <- jmat[, 1]
    > mat
             Apple    Orange Pear      Grape     Mango
     [1,] 2.284355 -0.612942   NA -0.2728387 -1.307932
     [2,]       NA        NA   NA         NA        NA
     [3,]       NA        NA   NA         NA        NA
     [4,]       NA        NA   NA         NA        NA
     [5,]       NA        NA   NA         NA        NA
     [6,]       NA        NA   NA         NA        NA
     [7,]       NA        NA   NA         NA        NA
     [8,]       NA        NA   NA         NA        NA
     [9,]       NA        NA   NA         NA        NA
    [10,]       NA        NA   NA         NA        NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, next PHPExcel question. I have an HTML form that users fill out and
I have question concerning a function I created. I would like to show the
i am learning Java at the moment and have the following question: i am
I have a small question. I have a file in the following format: 1
hello i have question in Python subprocess module I want to interaction with child
i have following question. I tried assignment by value and by reference and as
I've asked about this very example in another question: Fill in a form with
Hi; i have a question for calling wcf service. how to fill data array
Noob question : I have the following Forms layout (please excuse the JRuby syntax).
I thought this other SO thread would have answered my question (http://stackoverflow.com/questions/4883751/trouble-reading-json-object-in-python), as it

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.