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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:43:00+00:00 2026-06-15T07:43:00+00:00

I have a very large dataframe that I’d like to save a subset of

  • 0

I have a very large dataframe that I’d like to save a subset of based on a certain subset of a vector. In short I have something like this:

> id<-c("ID1","ID2","ID2","ID3","ID4","ID4","ID4","ID4","ID4")
> status<-c("flag","flag","none","none","flag","flag","flag","none","flag")
> misc1ofmany<-c("etc1","etc2","etc3","etc4","etc5","etc6","etc7","etc8","etc9")
> df = data.frame(id, status, misc1ofmany) ; df
   id status misc1ofmany
1 ID1   flag        etc1
2 ID2   flag        etc2
3 ID2   none        etc3
4 ID3   none        etc4
5 ID4   flag        etc5
6 ID4   flag        etc6
7 ID4   flag        etc7
8 ID4   none        etc8
9 ID4   flag        etc9

I’d like to have all the rows of IDs that have been flagged, inclusive of their non-flagged sessions. Right now I’m trying to get the index of the other IDs by grep and plugging that back into a new df. Actually as I wrote this out I figured out that grepl might be easier to work with:

> flaggedIDs <- unique(as.vector(df$id[grep("flag",df$status)]))
> flaggedIDs.allStats.Index <- mapply(grepl,df$id,MoreArgs=list(x=flaggedIDs)) 
> flaggedIDs.allStats.Index
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]
[1,]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[2,] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
[3,] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE

however I just want to get to here:

> flaggedIDsdf <- df[flaggedIDs.allStats.Index] ; flaggedIDsdf
   id status misc1ofmany
1 ID1   flag        etc1
2 ID2   flag        etc2
3 ID2   none        etc3
4 ID4   flag        etc5
5 ID4   flag        etc6
6 ID4   flag        etc7
7 ID4   none        etc8
8 ID4   flag        etc9

I feel like this should be simpler than I am making this out to be, however I’ve tried a number of possibilities to figure this out to no avail. writing out the question helped this become a clearer/simpler problem in my mind (it looks like I’m just missing one step now), but now I’m also wondering if there is a more efficient way of going about this.

  • 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-15T07:43:04+00:00Added an answer on June 15, 2026 at 7:43 am

    data.table is really useful here for it’s elegant syntax and memory efficiency

    library(data.table)
    
    DT <- data.table(df)
    
    setkey(DT, 'id')
    
    DT[DT[status=='flag', list(id = unique(id))]]
    
        id status misc1ofmany
    1: ID1   flag        etc1
    2: ID2   flag        etc2
    3: ID2   none        etc3
    4: ID4   flag        etc5
    5: ID4   flag        etc6
    6: ID4   flag        etc7
    7: ID4   none        etc8
    8: ID4   flag        etc9
    

    Or even more compactly

    DT[J(unique(id[status=='flag']))]
    

    These two approaches above use the fact that the i component is evaluated first by data.table.
    Keying by id means we can do a self-join to extract only those ids where status=='flag.


    Or, using by

    DT[,if(any(status=='flag')){.SD} ,by=id]
    

    This goes through the subsets of DT by id, and returns .SD (the data.table of the subset) if any(status=='flag') (within that subset).

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

Sidebar

Related Questions

I have a input dataframe like this (the real one is very large, so
I have a large data frame (named z ) that looks like this: RPos
I have very large dataframe and I need to choose variable number satisfying certain
I have 2 very large data sets that looks like below: merge_data <- data.frame(ID
I have a very large UIView approx 3000x3000 in size. In this large view
I have a very large possible data set that I am trying to visualize
I have a very large XML file which has like 40000 data, and when
I have a very large set of permissions in my application that I represent
I have a very large list Suppose I do that (yeah, I know the
I have very large string lists and arrays and i found 2 issues that

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.