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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:07:36+00:00 2026-06-10T18:07:36+00:00

I have a data.table and I need to extract equal length segments starting at

  • 0

I have a data.table and I need to extract equal length segments starting at various row locations. What is the easiest way to do this? For example:

x <- data.table(a=sample(1:1000,100), b=sample(1:1000,100))
r <- c(1,2,10,20,44)
idx <- lapply(r, function(i) {j <-which(x$a == i); if (length(j)>0) {return(j)} })
y <- lapply(idx, function(i) {if (!is.null(i)) x[i:(i+5)]})
do.call(rbind, y)
    a   b
1:  44  63
2:  96 730
3: 901 617
4: 446 370
5: 195 341
6: 298 411

This is certainly not the data.table way of doing things so I was hoping there is a better way?

EDIT: Per comments below, I edit this just so it’s clear that the values in a are not necessarily contiguous nor do they correspond to the row number.

  • 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-10T18:07:37+00:00Added an answer on June 10, 2026 at 6:07 pm

    Not sure whether you already know the row positions, or if you want to search for them. Either way, this should cover both.

    require(data.table)
    set.seed(1)
    DT = data.table(a=sample(1:1000,20), b=sample(1:1000,20))
    setkey(DT,a)
    DT
    #       a   b
    #  1:  62 338
    #  2: 175 593
    #  3: 201 267
    #  4: 204 478
    #  5: 266 935
    #  6: 372 212
    #  7: 374 711
    #  8: 380 184
    #  9: 491 659
    # 10: 572 651
    # 11: 625 863
    # 12: 657 380
    # 13: 679 488
    # 14: 707 782
    # 15: 760 816
    # 16: 763 404
    # 17: 894 385
    # 18: 906 126
    # 19: 940  14
    # 20: 976 107
    r = c(201,380,760)
    starts = DT[J(r),which=TRUE]  # binary search for items
                                  # skip if the starting row numbers are known
    starts
    # [1]  3  8 15
    

    Option 1: make the row number sequences, concatenate, and do one lookup in DT (no need for keys or binary search just to select by row numbers) :

    DT[unlist(lapply(starts,seq.int,length=5))]
    #       a   b
    #  1: 201 267
    #  2: 204 478
    #  3: 266 935
    #  4: 372 212
    #  5: 374 711
    #  6: 380 184
    #  7: 491 659
    #  8: 572 651
    #  9: 625 863
    # 10: 657 380
    # 11: 760 816
    # 12: 763 404
    # 13: 894 385
    # 14: 906 126
    # 15: 940  14
    

    Option 2: make a list of data.table subsets and then rbind them together. This is less efficient than option 1, but for completeness :

    L = lapply(starts,function(i)DT[seq.int(i,i+4)])
    L
    # [[1]]
    #      a   b
    # 1: 201 267
    # 2: 204 478
    # 3: 266 935
    # 4: 372 212
    # 5: 374 711
    # 
    # [[2]]
    #      a   b
    # 1: 380 184
    # 2: 491 659
    # 3: 572 651
    # 4: 625 863
    # 5: 657 380
    # 
    # [[3]]
    #      a   b
    # 1: 760 816
    # 2: 763 404
    # 3: 894 385
    # 4: 906 126
    # 5: 940  14 
    

    rbindlist(L)   # more efficient that do.call("rbind",L). See ?rbindlist.
    #       a   b
    #  1: 201 267
    #  2: 204 478
    #  3: 266 935
    #  4: 372 212
    #  5: 374 711
    #  6: 380 184
    #  7: 491 659
    #  8: 572 651
    #  9: 625 863
    # 10: 657 380
    # 11: 760 816
    # 12: 763 404
    # 13: 894 385
    # 14: 906 126
    # 15: 940  14
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table of correlation data that I need to extract. I
I have a table that I need to extract data from, and wish to
I have a table of data that I need to dynamically add a column
I have an Excel file that gets external data from database table. I need
I have table to test score data that I need to pivot and I
I have a table into which new data is frequently inserted. I need to
I have a table of contacts and a table of postcode data. I need
Hi I have a datatables based table that holds data. I need to be
I need to extract data from a third party system (I have no influence
I need to extract a table of data on a collection of pages. I

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.