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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:22:35+00:00 2026-06-05T19:22:35+00:00

Given a list a containing vectors of unequal length and a vector b containing

  • 0

Given a list a containing vectors of unequal length and a vector b containing some elements from the vectors in a, I want to get a vector of equal length to b containing the index in a where the element in b matches (this is a bad explanation I know)…

The following code does the job:

a <- list(1:3, 4:5, 6:9)
b <- c(2, 3, 5, 8)

sapply(b, function(x, list) which(unlist(lapply(list, function(y, z) z %in% y, z=x))), list=a)
[1] 1 1 2 3

Replacing the sapply with a for loop achieves the same of course

The problem is that this code will be used with list and vectors with a length above 1000. On a real life set the function takes around 15 seconds (both the for loop and the sapply).

Does anyone have an idea how to speed this up, safe for a parallel approach? I have failed to see a vectorized approach (and I cannot program in C, though that would probably be the fastest).

Edit:

Will just emphasize Aaron’s elegant solution using match() which gave a speed increase in the order of 1667 times (from 15 to 0.009)

I expanded a bit on it to allow multiple matches (the return is then a list)

a <- list(1:3, 3:5, 3:7)
b <- c(3, 5)
g <- rep(seq_along(a), sapply(a, length))
sapply(b, function(x) g[which(unlist(a) %in% x)])
[[1]]
[1] 1 2 3

[[2]]
[1] 2 3

The runtime for this was 0.169 which is arguably quite slower, but on the other hand more flexible

  • 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-05T19:22:36+00:00Added an answer on June 5, 2026 at 7:22 pm

    Here’s one possibility using match:

    a <- list(1:3, 4:5, 6:9)
    b <- c(2, 3, 5, 8)
    g <- rep(seq_along(a), sapply(a, length))
    g[match(b, unlist(a))]
    #> [1] 1 1 2 3
    

    findInterval is another option:

    findInterval(match(b, unlist(a)), cumsum(c(0, sapply(a, length))) + 1)
    #> [1] 1 1 2 3
    

    For returning a list, try this:

    a <- list(1:3, 4:5, 5:9)
    b <- c(2, 3, 5, 8, 5)
    g <- rep(seq_along(a), sapply(a, length))
    aa <- unlist(a)
    au <- unique(aa)
    af <- factor(aa, levels = au)
    gg <- split(g, af)
    gg[match(b, au)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a list of elements containing text: <root> <element>text text text ...</element> <element>text text
Given a tuple (specifically, a functions varargs), I want to prepend a list containing
Given an R list, I wish to find the index of a given list
Given a list of elements like so: int[] ia = new int[] { -4,
Given a list old_list = [obj_1, obj_2, obj_3, ...] I want to create a
I've got a function to return a list from an etree element, but it
I am trying to create a list containing the permutations of a given type
You are given a file containing a list of strings (one per line). The
Given is a list containing all but 2 numbers between 1-20 (randomly ordered). I
Given a list of objects containing two properties (IdentityType and Name) in the format:

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.