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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:42:41+00:00 2026-05-26T14:42:41+00:00

Apologies is this is something a more seasoned R user would know, but I

  • 0

Apologies is this is something a more seasoned R user would know, but I just came across this and wanted to ask about proper usage.

It appears to be possible to classify ranges for variables by using as.factor. So, I could group observations into a range. For example, if I were looking at visits by user, it looks that I could write an if/then statement to bin the users by the range of visits they had, then get summary statistics based on the group.

Here is the link where I learned about this: http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-2.html

Now, while this function looks easier than grouping data by using plyr and ddply, it does not look to be powerful enough to break the variable into X number of bins (for example 10 for a decile) – You would have to do that yourself.

This leads to my question – Is one better than the other for grouping data, or are there just many ways to tackle grouping like this?

Thanks

  • 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-26T14:42:42+00:00Added an answer on May 26, 2026 at 2:42 pm

    I think cut is a better tool for this.

    With some sample data:

    set.seed(123)
    age <- round(runif(10,20,50))
    

    This is what I’d do:

    > cut(age, c(0,30,40,Inf))
     [1] (0,30]   (40,Inf] (30,40]  (40,Inf] (40,Inf] (0,30]   (30,40]  (40,Inf]
     [9] (30,40]  (30,40] 
    Levels: (0,30] (30,40] (40,Inf]
    

    Optionally, setting the factor labels manually:

    > cut(age, c(0,30,40,Inf), labels=c('0-30', '31-40', '40+'))
     [1] 0-30  40+   31-40 40+   40+   0-30  31-40 40+   31-40 31-40
    Levels: 0-30 31-40 40+
    

    To contrast, the linked page suggests this:

    > as.factor(ifelse(age<=30, '0-30', ifelse(age <= 40, '30-40', '40+')))
     [1] 0-30  40+   30-40 40+   40+   0-30  30-40 40+   30-40 30-40
    Levels: 0-30 30-40 40+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologise if this is a bit dim but I want to send something
Apologies if this is a slightly noob question, but looking to clarify my thoughts
I know this is bit of a strange one but if anyone had any
It seems the more I talk about this problem the better I understand it.
Apologies if this is a simple question, I'm still pretty new to this, but
My apologies for the post. I realize that this is more than likely redundant
My apologies if this is something obvious that I have missed - strange enough
First off, apologies if this question has been asked before but I couldn't find
This is probably a really easy question, so apologies for not being more challenging!
Firstly apologies if this is a really simple question but Git is absolutely brand

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.