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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:03:58+00:00 2026-06-16T08:03:58+00:00

I have a matrix from which I would like to select an observation together

  • 0

I have a matrix from which I would like to select an observation together with its column name.

If I use subset() or matrix[]%in% then I get the whole row.

I would only like to get that single observation and its column name.
Is there a way to do this?

Here’s an example:

matrix:

        col1 col2 col3
row 1    10   20    30 
row 2    30   30    40

Now I would like to select the value and column name with the highest value:

max <- max(matrix)
subset(matrix, matrix==max)

This gives the output:

        col1 col2 col3
row 2    30   30    40

However what I would like to get is:

       col3
        40
  • 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-16T08:04:00+00:00Added an answer on June 16, 2026 at 8:04 am

    As you already know the value, you just need to get the column names.

    Calling your matrix m for the obvious reason:

    m <- matrix(c(10,30,20,30,30,40), ncol=3)
    colnames(m) <- c('col1','col2','col3')
    

    To see which columns contain the max (or any particular value), and their names, apply can be used to return a logical vector with names:

    apply(m==10, 2, any)
    col1  col2  col3 
    TRUE FALSE FALSE 
    
    apply(m==30, 2, any)
    col1 col2 col3 
    TRUE TRUE TRUE 
    

    which tells you which values are true:

    which(apply(m==40, 2, any))
    col3 
       3 
    

    And just the names, if that is what you need:

    names(which(apply(m==40, 2, any)))
    [1] "col3"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large matrix from which I would like to gather a collection
I have a large matrix from which I would like to randomly extract a
I have matrix (a) with (1:10),<10 x 1> double. I would like to copy
I would like to use a match with function to compare two matrix. I
I have an object with which I would like to make follow a bezier
I have a MxN Matrix and would like to convert into a vector MNx1
I have a large m *n sparse matrix Y. I would like to normalize
I would like to build a general matrix module, which includes such general operations
I need to get rotation matrix from direction vector (vForward) I also have vRight
I have NxM matrix with integer elements, greater or equal than 0. From any

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.