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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:37:45+00:00 2026-06-06T15:37:45+00:00

How can I subset the following example data frame to only return one observation

  • 0

How can I subset the following example data frame to only return one
observation for the earliest occurance [i.e. min(year)] of each id?

id <- c("A", "A", "C", "D", "E", "F")
year <- c(2000, 2001, 2001, 2002, 2003, 2004)
qty  <- c(100, 300, 100, 200, 100, 500)
df=data.frame(year, qty, id)

In the example above there are two observations for the “A” id at years 2000 and 2001. In the case of duplicate id’s, I would like the subset data frame to only include the the first occurance (i.e. at 2000) of the observations for the duplicate id.

df2 = subset(df, ???)

This is what I am trying to return:

df2

year qty id
2000 100  A
2001 100  C
2002 200  D
2003 100  E
2004 500  F

Any assistance would be greatly appreciated.

  • 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-06T15:37:47+00:00Added an answer on June 6, 2026 at 3:37 pm

    You can aggregate on minimum year + id, then merge with the original data frame to get qty:

    df2 <- merge(aggregate(year ~ id, df1, min), df1)
    
    # > df2
    #   id year qty
    # 1  A 2000 100
    # 2  C 2001 100
    # 3  D 2002 200
    # 4  E 2003 100
    # 5  F 2004 500
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a property column which can have a subset of the following values
How can return a subset of a FormCollection with key/values in tact for those
I have the following matrix (actually a data frame) in R: > str(x2) 'data.frame':
I am trying to translate a subset of Java to c++. How can i
The problem: I often need to select a set of variables from a data.frame
I am trying to achieve the following: Have one 'full' set of services for
Consider the following problem: There are N coins numbered 1 to N. You can't
I am trying to achieve following things: To create subset of original font file
Consider the following situation: students receive grades for different topics, where n -th-year students
I have a C# collection of strings. Each string is a sentence that can

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.