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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:59:35+00:00 2026-05-20T13:59:35+00:00

I like to select the first (2,3,0,4) rows of each group in a data

  • 0

I like to select the first (2,3,0,4) rows of each group in a data frame.

> f<-data.frame(group=c(1,1,1,2,2,3,4),y=c(1:7))
> 
>   group y
>      1 1
>      1 2
>      1 3
>      2 4
>      2 5
>      3 6
>      4 7

and obtain a data frame as follows

group y
1 1
1 2
2 4
2 5
4 7

I tried to use by and head but head does not take a vector.

Thank you for your help.

  • 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-05-20T13:59:35+00:00Added an answer on May 20, 2026 at 1:59 pm

    Version of function with indexes.

    fun1 <- function(){
      idx <- c(0,which(diff(f$group)!=0))+1
      idx2 <- unlist(lapply(1:length(nf),function(x)  seq.int(from=idx[x],length.out=nf[x])),use.names=F)
      f1 <- f[idx2,]
      return(f1)
    }
    
    fun2 <- function(){
      ddply(f,.(group),function(x) head(x,nf[x[1,1]]))
    }
    

    Test data (size suggested by author of question)

    f<-data.frame(group=sample(1:1000,50000,T),y=c(1:50000))
    f <- f[order(f$group),]
    nf <- rpois(length(unique(f$group)),3) 
    

    system.time(fun1())
    system.time(fun2())

    On my system ~60 times faster is fun1.

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

Sidebar

Related Questions

I'd like to select the first row of each set of rows grouped with
the row source for a listbox looks like this: SELECT users.id, users.first, users.last, chavrusas.luser_type
Getting error when I tried like this: $ci =& get_instance(); $ci->db->select(CONCAT_WS(' ',users.name_first,users.name_last) AS user_name,CONCAT_WS('
I would like get 3 random stamps, but each from different country. This query
For internal control we would like to select a single random invoice for each
I have a scenario where I would like to select rows from a table
Something like: SELECT MONTH(*date?*) FROM //Not sure what goes here LIMIT 0,6 I know
I like select the two inputbox after the automplete box. The problem is that
Given a query like: SELECT table1.field1 FirstField, table2.field2 SecondField FROM table1 INNER JOIN table2
My query string is like: SELECT ... FROM maintable LEFT JOIN table1 on (maintable.id

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.