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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:06:37+00:00 2026-06-14T00:06:37+00:00

From a dataframe like this test <- data.frame(‘id’= rep(1:5,2), ‘string’= LETTERS[1:10]) test <- test[order(test$id),

  • 0

From a dataframe like this

test <- data.frame('id'= rep(1:5,2), 'string'= LETTERS[1:10])
test <- test[order(test$id), ]
rownames(test) <- 1:10

> test
    id string
 1   1      A
 2   1      F
 3   2      B
 4   2      G
 5   3      C
 6   3      H
 7   4      D
 8   4      I
 9   5      E
 10  5      J

I want to create a new one with the first row of each id / string pair. If sqldf accepted R code within it, the query could look like this:

res <- sqldf("select id, min(rownames(test)), string 
              from test 
              group by id, string")

> res
    id string
 1   1      A
 3   2      B
 5   3      C
 7   4      D
 9   5      E

Is there a solution short of creating a new column like

test$row <- rownames(test)

and running the same sqldf query with min(row)?

  • 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-14T00:06:38+00:00Added an answer on June 14, 2026 at 12:06 am

    You can use duplicated to do this very quickly.

    test[!duplicated(test$id),]
    

    Benchmarks, for the speed freaks:

    ju <- function() test[!duplicated(test$id),]
    gs1 <- function() do.call(rbind, lapply(split(test, test$id), head, 1))
    gs2 <- function() do.call(rbind, lapply(split(test, test$id), `[`, 1, ))
    jply <- function() ddply(test,.(id),function(x) head(x,1))
    jdt <- function() {
      testd <- as.data.table(test)
      setkey(testd,id)
      # Initial solution (slow)
      # testd[,lapply(.SD,function(x) head(x,1)),by = key(testd)]
      # Faster options :
      testd[!duplicated(id)]               # (1)
      # testd[, .SD[1L], by=key(testd)]    # (2)
      # testd[J(unique(id)),mult="first"]  # (3)
      # testd[ testd[,.I[1L],by=id] ]      # (4) needs v1.8.3. Allows 2nd, 3rd etc
    }
    
    library(plyr)
    library(data.table)
    library(rbenchmark)
    
    # sample data
    set.seed(21)
    test <- data.frame(id=sample(1e3, 1e5, TRUE), string=sample(LETTERS, 1e5, TRUE))
    test <- test[order(test$id), ]
    
    benchmark(ju(), gs1(), gs2(), jply(), jdt(),
        replications=5, order="relative")[,1:6]
    #     test replications elapsed relative user.self sys.self
    # 1   ju()            5    0.03    1.000      0.03     0.00
    # 5  jdt()            5    0.03    1.000      0.03     0.00
    # 3  gs2()            5    3.49  116.333      2.87     0.58
    # 2  gs1()            5    3.58  119.333      3.00     0.58
    # 4 jply()            5    3.69  123.000      3.11     0.51
    

    Let’s try that again, but with just the contenders from the first heat and with more data and more replications.

    set.seed(21)
    test <- data.frame(id=sample(1e4, 1e6, TRUE), string=sample(LETTERS, 1e6, TRUE))
    test <- test[order(test$id), ]
    benchmark(ju(), jdt(), order="relative")[,1:6]
    #    test replications elapsed relative user.self sys.self
    # 1  ju()          100    5.48    1.000      4.44     1.00
    # 2 jdt()          100    6.92    1.263      5.70     1.15
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to filter rows from a data.frame based on a logical condition. Let's
I have a data.frame from this code: my_df = data.frame(read_time = c(2010-02-15, 2010-02-15, 2010-02-16,
I have a data.frame like this: (t=structure(list(count = c(NA, 2, NA, NA, NA, 8,
I want to read a dataframe from a fixed width flat file. This is
I'm having a dataframe in which I'm reading from a test file as like
I have a dataframe that looks like this: <class 'pandas.core.frame.DataFrame'> DatetimeIndex: 2016910 entries, 2009-01-02
I often need to remove lists of columns from a data.frame. I usually do
I am looking for a more versatile way to get from a data.frame to
I am trying to sample a data frame from a given data frame such
I have a data frame consisting of results from multiple runs of an experiment,

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.