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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:07:51+00:00 2026-06-06T12:07:51+00:00

I am having a hard time trying to subset a data.table (package) in R.

  • 0

I am having a hard time trying to subset a data.table (package) in R. Giving a following example

library(data.table)

x = c(rep("a", 6), rep("b", 5))
y = c(0,2,1,0,1,2, 0,1,0,2,1)
z = c(1:6,1:5) + rnorm(11, 0.02, 0.1)

DT = data.table(ind = x, cond = y, dist = z)

      ind cond     dist
 [1,]   a    0 1.078966
 [2,]   a    2 1.987159
 [3,]   a    1 3.143391
 [4,]   a    0 3.937058
 [5,]   a    1 5.037681
 [6,]   a    2 6.036432
 [7,]   b    0 1.057809
 [8,]   b    1 2.144755
 [9,]   b    0 3.010903
[10,]   b    2 3.937765
[11,]   b    1 4.976273

I want to subset everything after the first 1 in cond column. In other words, everything that is larger than 3.143391 for a and 2.144755 for b (in this example).

DT.sub <- DT[cond == "1",] # Please, combine this row
DT.sub[,.SD[dist==min(dist)],by=ind] # With this to make the code shorter, if you can.

  ind cond     dist
[1,]   a    1 3.143391
[2,]   b    1 2.144755

The result should look like this:

      ind cond     dist
 [1,]   a    0 3.937058
 [2,]   a    1 5.037681
 [3,]   a    2 6.036432
 [4,]   b    0 3.010903
 [5,]   b    2 3.937765
 [6,]   b    1 4.976273
  • 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-06T12:07:53+00:00Added an answer on June 6, 2026 at 12:07 pm

    How about :

    DT[,.SD[seq(match(1,cond)+1,.N)],by=ind]
         ind cond     dist 
    [1,]   a    0 3.937058 
    [2,]   a    1 5.037681 
    [3,]   a    2 6.036432 
    [4,]   b    0 3.010903 
    [5,]   b    2 3.937765 
    [6,]   b    1 4.976273 
    

    Btw, it’s good to set.seed(1) first so we can work with the same random data.

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

Sidebar

Related Questions

I'm having hard time trying to figure out how to auto-save user data in
I'm having hard time trying to set certain cell's value in table. Here is
I'm having a hard time trying to use .strip with the following line of
I am having a hard time trying to figure out if the following design
I am having a hard time trying to figure out which data type I
I'm having a hard time trying to tar some files using the compress library.
I'm porting a C# library to COM and I'm having a hard time trying
I'm having hard time trying to force https (ssl) to particular page on codeiginiter
I am having a hard time trying to create and populate on the fly
I'm having a hard time trying to get hasMany to work in Grails 2.0.1

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.