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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:05:26+00:00 2026-06-13T08:05:26+00:00

Overview Give a large (nrows > 5,000,000+) data frame, A , with string row

  • 0

Overview

Give a large (nrows > 5,000,000+) data frame, A, with string row names and a list of disjoint sets (n = 20,000+), B, where each set consists of row names from A, what is the best way to create a vector representing the sets in B via a unique value?

Illustration

Below is an example illustrating this problem:

# Input
A <- data.frame(d = rep("A", 5e6), row.names = as.character(sample(1:5e6)))
B <- list(c("4655297", "3177816", "3328423"), c("2911946", "2829484"), ...) # Size 20,000+

The desired result would be:

# An index of NA represents that the row is not part of any set in B.
> A[,"index", drop = F]
        d index
4655297 A     1
3328423 A     1
2911946 A     2
2829484 A     2
3871770 A    NA
2702914 A    NA
2581677 A    NA
4106410 A    NA
3755846 A    NA
3177816 A     1

Naive Attempt

Something like this can be achieved using the following method.

n <- 0
A$index <- NA
lapply(B, function(x){
  n <<- n + 1
  A[x, "index"] <<- n
})

Problem

However this is unreasonably slow (several hours) due to indexing A multiple times and is not very R-esque or elegant.

How can the desired result be generated in a quick and efficient manner?

  • 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-13T08:05:27+00:00Added an answer on June 13, 2026 at 8:05 am

    Here is a suggestion using base that isn’t too bad when compared to your current method.

    Sample data:

    A <- data.frame(d   = rep("A", 5e6),
                    set = sample(c(NA, 1:20000), 5e6, replace = TRUE),
                    row.names = as.character(sample(1:5e6)))
    B <- split(rownames(A), A$set)
    

    Base method:

    system.time({
    A$index <- NA
    A[unlist(B), "index"] <- rep(seq_along(B), times = lapply(B, length))
    })
    #    user  system elapsed 
    #   15.30    0.19   15.50 
    

    Check:

    identical(A$set, A$index)
    # TRUE
    

    For anything faster, I suppose data.table will come handy.

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

Sidebar

Related Questions

Can someone give an overview of mixing Jpct and Qualcomm Vuforia SDK? Is it
Could someone give me a quick overview of the pros and cons of using
Can someone give me a quick, one-to-few paragraph(s) overview of the structure of iPhone
To make the explanation easier I'll give a quick overview of the program. I'm
Quick overview: trying to build a gallery with a string from $_GET ('foo'), which
To give a quick overview of the program 1.) Opens connection to a socket
Can anyone give a short overview of the cases when and how an activity
The title should give a fair overview of the problem but I'm running a
Does anyone know of any good tutorials/articles which give a broad overview of QT?
Could somebody give me a brief overview of the differences between HTTP 1.0 and

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.