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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:04:30+00:00 2026-06-09T23:04:30+00:00

I have a vector nodenames as nodenames <- c(A,B,C,T,N,Z) I have a square sparse

  • 0

I have a vector nodenames as

nodenames <- c("A","B","C","T","N","Z")

I have a square sparse matrix with dimnames as

Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:4149962] 1 2 3 4 5 9 11 12 13 14 ...
  ..@ p       : int [1:3417] 0 1702 2710 3935 5411 6719 8141 9822 9822 11515 ...
  ..@ Dim     : int [1:2] 3416 3416
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:3416] "A" "B" "AAL" "T" ...
  .. ..$ : chr [1:3416] "A" "B" "AAL" "T" ...
  ..@ x       : num [1:4149962] 2 1 1 3 1 1 2 19 3 2 ...
  ..@ factors : list()

How can I produce a subset of this matrix with dimnames in nodenames?

  • 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-09T23:04:31+00:00Added an answer on June 9, 2026 at 11:04 pm

    I think Tim Riffe’s answer is the most direct. If the user were unsure whether the ‘nodenames’ vector were a subset of both the rownames() and the colnames() values, then this might be a bit safer:

    nodenames <- c("A","ZZ","C","T","N","Z")
    
    seq1 <- seq(1:100)
    mat1 <- matrix(seq1, 10)
    rownames(mat1)<-c("G","A","B","F","C","D","T","N","Z","J")
    colnames(mat1)<-c("G","A","B","F","C","D","T","N","Z","J")
    mat1[rownames(mat1) %in% nodenames, colnames(mat1) %in% nodenames]
    #----------
       A  C  T  N  Z
    A 12 42 62 72 82
    C 15 45 65 75 85
    T 17 47 67 77 87
    N 18 48 68 78 88
    Z 19 49 69 79 89
    

    For the amended question for objects of class-dgCMatrix I am getting sensible results using the same methods:

    (m <- Matrix(c(0,0,2:0), 3,5))
    3 x 5 sparse Matrix of class "dgCMatrix"
    
    [1,] . 1 . . 2
    [2,] . . 2 . 1
    [3,] 2 . 1 . .
    
     m@Dimnames <- list(X=letters[1:3], Y=LETTERS[1:5])
     m["a", "B"]
    # [1] 1
     m["a", c("A","B")]
    # A B 
    # 0 1 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
I have a vector class and I defined the __mul__ method to multiply a
I have a vector<int> with 10,000,000 (10 million) elements, and that my workstation has
I have Vector class, representing a 3D point, written as follows in Vector.h :
I have a vector class that has addition, multiplication, subtraction, division, etc operators. I'm
Lets say i have vector of vectors vector< vector<int> > bigTable; vector<int> data; data.resize(2);
I have a vector of smart ptr of my class Foo: struct Foo {
I can think of three ways of filling a std::vector Suppose we have vector<int>
I have a vector-like class that contains an array of objects of type T
I have a vector of type template class and I am trying to print

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.