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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:23:42+00:00 2026-05-22T12:23:42+00:00

I am new to R and am trying to find a better solution for

  • 0

I am new to R and am trying to find a better solution for accomplishing this fairly simple task efficiently.

I have a data.frame M with 100,000 lines (and many columns, out of which 2 columns are relevant to this problem, I’ll call it M1, M2). I have another data.frame where column V1 with about 10,000 elements is essential to this task. My task is this:

For each of the element in V1, find where does it occur in M2 and pull out the corresponding M1. I am able to do this using for-loop and it is terribly slow! I am used to Matlab and Perl and this is taking for EVER in R! Surely there’s a better way. I would appreciate any valuable suggestions in accomplishing this task…

for (x in c(1:length(V$V1)) {  
    start[x] = M$M1[M$M2 == V$V1[x]]  
}  

There is only 1 element that will match, and so I can use the logical statement to directly get the element in start vector. How can I vectorize this?

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-05-22T12:23:42+00:00Added an answer on May 22, 2026 at 12:23 pm

    Here is another solution using the same example by @aix.

    M[match(V$V1, M$M2),]

    To benchmark performance, we can use the R package rbenchmark.

    library(rbenchmark)
    f_ramnath = function() M[match(V$V1, M$M2),]
    f_aix = function() merge(V, M, by.x='V1', by.y='M2', sort=F)
    f_chase = function() M[M$M2 %in% V$V1,] # modified to return full data frame
    
    benchmark(f_ramnath(), f_aix(), f_chase(), replications = 10000)
         test replications elapsed relative
    2     f_aix()        10000  12.907 7.068456
    3   f_chase()        10000   2.010 1.100767
    1 f_ramnath()        10000   1.826 1.000000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to Git and trying my best to find some documentation
I'm trying to find the new line and returns that are inside an nsdata
I'm new to three20 and trying to find my way through the TTNavigator class.
I'm new at MVC and I'm trying to find a strong architecture for a
I'm new to rake and I'm trying to find my way in automating some
Ok Im still new to Zend, and trying to find how the ORM works
I'm a new Emacs user, and I'm trying to find a good way to
I trying to navigate my new EC2 setup and was hoping to find an
I've spent the better part of the day trying to find an answer to
I'm trying to find an elegant OOP solution for the following problem. Assume we

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.