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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:43:02+00:00 2026-05-27T10:43:02+00:00

I have a matrix X1 with 6 columns. Column 3 in this X1 matrix

  • 0

I have a matrix X1 with 6 columns. Column 3 in this X1 matrix contains RouteNo. I also have a vector V1 which is extracted from another matrix. Few values from this vector matches with RouteNo in X1. The task is to take a subset from matrix X1 where RouteNo from X1 matches with RouteNo from V1. V1 contains extra RouteNo than in matrix X1.

> X1
    V1 V2       V3 V4   V5 V6
1    1  2 84072082  1 2000  0
2    2  2 84046006  1 2000  0
3    3  2 84046006  1 2001  0
4    4  2 84046006  1 2002  0
5    5  2 84021002  1 2002  0
6    6  2 84021002  1 2003  0
7    7  2 84021002  1 2003  0
8    8  2 84021002  1 2004  0
9    9  2 84021002  1 2005  0
10  10  2 84021002  1 2005  0
11  11  2 12468015  1 2006  0
12  12  2 12468015  1 2007  0
13  96  2 12468015  2 2000  0
> V1
 [1] 84021001 84021002 84021105 84046006 84046007 84046008 84046009 84046011 84046013 84046014
> n2 = subset(X1, subset = X1[,3] %in% V1)
> dim(n2)
[1] 0 6

I tried using subset function but I am not getting the desired result. I expect to get the matrix as below

2    2  2 84046006  1 2000  0
3    3  2 84046006  1 2001  0
4    4  2 84046006  1 2002  0
5    5  2 84021002  1 2002  0
6    6  2 84021002  1 2003  0
7    7  2 84021002  1 2003  0
8    8  2 84021002  1 2004  0
9    9  2 84021002  1 2005  0

Is there any other way to get the result? Any help is appreciated. Thank in advance.

  • 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-05-27T10:43:02+00:00Added an answer on May 27, 2026 at 10:43 am

    You are running into problems with scoping. You have a column named V1 in your data.frame x1. Change your look up vector to a name that isn’t a column name and everything should be fine, i.e.:

    subset(x1, V3 %in% v1)
    

    or use [ to index directly

    x1[x1$V3 %in% V1,]
    

    The proof is in the pudding:

    txt1 <- "    V1 V2       V3 V4   V5 V6
    1    1  2 84072082  1 2000  0
    2    2  2 84046006  1 2000  0
    3    3  2 84046006  1 2001  0
    4    4  2 84046006  1 2002  0
    5    5  2 84021002  1 2002  0
    6    6  2 84021002  1 2003  0
    7    7  2 84021002  1 2003  0
    8    8  2 84021002  1 2004  0
    9    9  2 84021002  1 2005  0
    10  10  2 84021002  1 2005  0
    11  11  2 12468015  1 2006  0
    12  12  2 12468015  1 2007  0
    13  96  2 12468015  2 2000  0"
    txt2 <- "84021001 84021002 84021105 84046006 84046007 84046008 84046009 84046011 84046013 84046014"
    
    x1 <- read.table(textConnection(txt1))
    #Note the lowercase
    v1 <- read.table(textConnection(txt2))
    #Make "V1" as you have it
    V1 <- v1 
    
    > #Bad
    > dim(subset(x1, V3 %in% V1))
    [1] 0 6
    > #Good
    > dim(subset(x1, V3 %in% v1))
    [1] 9 6
    #Does subset method equal the direct indexing method
    > all.equal(subset(x1, V3 %in% v1),x1[x1$V3 %in% V1,])
    [1] TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have one matrix with two columns. Every column has thousands of values.
Let's say I have a matrix x which contains 10 rows and 2 columns.
I have a dataframe. first column contains my the predictive score (range from 0
I have a mathematical matrix class. It contains a member function which is used
Suppose I have a two column matrix. How do I pack the columns into
I have a matrix (5x10000) with the fifth line contains values ​​between 1 and
I have a SSRS matrix table, which generates column dynamically. but i want to
I have a 4x4 matrix which I wish to decompose into 4 frequency bands
I have a listview control, to this control I am adding rows which contain
I have a std::istream which refers to matrix data, something like: 0.0 1.0 2.0

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.