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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:17:08+00:00 2026-06-16T02:17:08+00:00

Suppose I have a matrix A with N columns, and I take 2 (or

  • 0

Suppose I have a matrix A with N columns, and I take 2 (or any subset) of columns from this matrix to construct a new matrix B, for instance:

B = cbind(A[,1], A[,3]) 

Is there a simple argument I can add so that the header name for the two columns is transferred? Using names(B) = names(A) won’t work because the matrices are not the same dimension.

  • 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-16T02:17:09+00:00Added an answer on June 16, 2026 at 2:17 am

    An example would help greatly since I suspect you may be using a dataframe which you are incorrectly calling a matrix. I say that because the names<- function used with a matrix would destroy the matrix structure. The proper function to modify column names is colnames<-. Furthermore if you were extracting the columns from a matrix using the “[” function there is almost no way the the column names would not come across with the values:

    > mat <- matrix(1:9, 3)
    > colnames(mat) <- letters[1:3]
    > mat[ , 2:3]
         b c
    [1,] 4 7
    [2,] 5 8
    [3,] 6 9
    

    Responding to your comment, it would be better to do this:

    B <- A[ , c(1,3) ]
    

    Then your column names would be properly carried over. (Note added: I was surprised that your cbind operation did not bring the col.names over and wondered why that was so. This version of using “[” with cbind does retain the col.names:

    > B=cbind( A[,1,drop=FALSE], A[,3,drop=FALSE])
    > B
         a c
    [1,] 1 7
    [2,] 2 8
    [3,] 3 9
    

    The “[” function will coerce single columns or rows to an atomic vector and apparently also looses its dimnames attribute. drop=FALSE prevents that loss.

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

Sidebar

Related Questions

Suppose I have a matrix foo as follows: foo <- cbind(c(1,2,3), c(15,16,17)) > foo
Suppose that I have a 2D array (matrix) in Java like this... int[][] MyMat
Suppose I have a matrix with n columns and m rows. In my case
Suppose i have a matrix like this table = [ # grid: 4 by
Suppose I have a NxN matrix M (lil_matrix or csr_matrix) from scipy.sparse, and I
Suppose I have a two column matrix. How do I pack the columns into
I want to build a square matrix. Let's suppose We have this matrix called
Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
Suppose I have a matrix of weights, and another matrix of data values. Can
suppose I have the following matrix a = 2 NaN NaN 4 NaN 3

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.