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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:28:29+00:00 2026-06-14T15:28:29+00:00

I’m quite new to R, I use it mainly for visualising statistics using ggplot2

  • 0

I’m quite new to R, I use it mainly for visualising statistics using ggplot2 library. Now I have faced a problem with data preparation.

I need to write a function, that will remove some number (2, 5 or 10) rows from a data frame that have highest and lowest values in specified column and put them into another data frame, and do this for each combination of two factors (in my case: for each day and server).

Up to this point, I have done the following steps (MWE using esoph example dataset).

I have sorted the frame according to the desired parameter (ncontrols in example):

esoph<-esoph[with(esoph,order(-ncontrols)) ,]

I can display first/last records for each factor value (in this example for each age range):

by(data=esoph,INDICES=esoph$agegp,FUN=head,3)
by(data=esoph,INDICES=esoph$agegp,FUN=tail,3)

So basically, I can see the highest and lowest values, but I don’t know how to extract them into another data frame and how to remove them from the main one.

Also in the above example I can see top/bottom records for each value of one factor (age range), but in reality I need to know highest and lowest records for each value of two factors — in this example they could be agegp and alcgp.

I am not even sure if these above steps are OK – perhaps using plyr would work better? I’d appreciate any hints.

  • 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-14T15:28:31+00:00Added an answer on June 14, 2026 at 3:28 pm

    Yes, you can use plyr as follows:

    esoph <- data.frame(agegp = sample(letters[1:2], 20, replace = TRUE),
                        alcgp = sample(LETTERS[1:2], 20, replace = TRUE),
                        ncontrols = runif(20))
    
    ddply(esoph, c("agegp", "alcgp"),
          function(x){idx <- c(which.min(x$ncontrols),
                               which.max(x$ncontrols))
                      x[idx, , drop = FALSE]})
    #   agegp alcgp  ncontrols
    # 1     a     A 0.03091483
    # 2     a     A 0.88529790
    # 3     a     B 0.51265447
    # 4     a     B 0.86111649
    # 5     b     A 0.28372232
    # 6     b     A 0.61698401
    # 7     b     B 0.05618841
    # 8     b     B 0.89346943
    
    ddply(esoph, c("agegp", "alcgp"),
          function(x){idx <- c(which.min(x$ncontrols),
                               which.max(x$ncontrols))
                      x[-idx, , drop = FALSE]})
    #    agegp alcgp ncontrols
    # 1      a     A 0.3745029
    # 2      a     B 0.7621474
    # 3      a     B 0.6319013
    # 4      b     A 0.3055078
    # 5      b     A 0.5146028
    # 6      b     B 0.3735615
    # 7      b     B 0.2528612
    # 8      b     B 0.4415205
    # 9      b     B 0.6868219
    # 10     b     B 0.3750102
    # 11     b     B 0.2279462
    # 12     b     B 0.1891052
    

    There are possibly many alternatives, e.g. using head and tail if your data is already sorted, but this should work.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have been unable to fix a problem with Java Unicode and encoding. The
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the

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.