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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:27:16+00:00 2026-06-08T16:27:16+00:00

Following some great advice from before , I’m now writing my 2nd R function

  • 0

Following some great advice from before, I’m now writing my 2nd R function and using a similar logic. However, I’m trying to automate a bit more and may be getting too smart for my own good.

I want to break the clients into quintiles based on the number of orders. Here’s my code to do so:

# sample data
clientID <- round(runif(200,min=2000, max=3000),0)
orders <- round(runif(200,min=1, max=50),0)

df <- df <- data.frame(cbind(clientID,orders))

#function to break them into quintiles
ApplyQuintiles <- function(x) {
  cut(x, breaks=c(quantile(df$orders, probs = seq(0, 1, by = 0.20))), 
      labels=c("0-20","20-40","40-60","60-80","80-100"))
}

#Add the quintile to the dataframe
df$Quintile <- sapply(df$orders, ApplyQuintiles)

table(df$Quintile)

0-20   20-40   40-60    60-80   80-100 
40     39      44       38      36

You’ll see here that in my sample data, I created 200 observations, yet only 197 are listed via table. The 3 left off are NA

Now, there are some clientIDs that have an ‘NA’ for quintile. It seems if they were at the lowest break, in this case, 1, then they were not included in the cut function.

Is there a way to make cut inclusive of all observations?

  • 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-08T16:27:18+00:00Added an answer on June 8, 2026 at 4:27 pm

    Try the following:

    set.seed(700)
    
    clientID <- round(runif(200,min=2000, max=3000),0)
    orders <- round(runif(200,min=1, max=50),0)
    
    df <- df <- data.frame(cbind(clientID,orders))
    
    ApplyQuintiles <- function(x) {
      cut(x, breaks=c(quantile(df$orders, probs = seq(0, 1, by = 0.20))), 
          labels=c("0-20","20-40","40-60","60-80","80-100"), include.lowest=TRUE)
    }
    df$Quintile <- sapply(df$orders, ApplyQuintiles)
    table(df$Quintile)
    
    0-20  20-40  40-60  60-80 80-100 
      40     41     39     40     40 
    

    I included include.lowest=TRUE in your cut function, which seems to make it work. See ?cut for more details.

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

Sidebar

Related Questions

Really new and learning C# and following along some training video from PluralSight. Great
following some example from Microsoft I created my custom MembershipProvider in my MVC application
I tried following some of the code from here and here , but I
Im trying to hammer togehter a WYSIWYG-edit in c# following some examples from here
I am mainly from a Sql Server background, and following some issues with getting
I need to check whether a string contains any swear words. Following some advice
Following is some codes and output from the Chrome Developers' Console Case 1: var
I'm looking for some advice. My boss is writing a series of lengthy articles.
All, Have used this site a few times before and had some great replies
I'm looking for some advice from the community about constructing an online store based

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.