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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:52:27+00:00 2026-06-11T08:52:27+00:00

I’d like to classify the values of a data frame according to two columns.

  • 0

I’d like to classify the values of a data frame according to two columns. Let’s say, I’ve got the following data frame:

my.df <- data.frame(a=c(1:20), b=c(61:80))

And now I want to subdivide it into 8 areas by dividing the 2D-scatterplot into 4 equal parts and then overlaying a rectangle in the middle that would consist of a quarter of each of the 4 parts. So far I’ve been using the following tedious way:

ar <- range(my.df$a)
br <- range(my.df$b)

aint <- seq(ar[1], ar[2], by=(ar[2]-ar[1])/4)
bint <- seq(br[1], br[2], by=(br[2]-br[1])/4)

my.df$z <- NA
my.df[which(my.df$a < aint[3] & my.df$b < bint[3]),"z"] <- 1
my.df[which(my.df$a < aint[3] & my.df$b >= bint[3]),"z"] <- 2
...
my.df[which(my.df$z == 1 & my.df$a >= aint[2] & my.df$b >= bint[2]),"z"] <- 5
...

I am sure there must be a way to do it in a neater and more general way, i.e. by writing a general function, but I am struggling to write one myself.

Also, I was surprised to see that after all of this, the class of the column z is automatically set to shingle. Why that? How does R “know” that this is a shingle?

  • 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-11T08:52:28+00:00Added an answer on June 11, 2026 at 8:52 am

    I’d approach it by cutting it into 16 groups first (x and y into 4 groups independently) and then combining them back together into fewer groups.

    my.df$a.q <- cut(my.df$a, breaks=4, labels=1:4)
    my.df$b.q <- cut(my.df$b, breaks=4, labels=1:4)
    my.df$a.b.q <- paste(my.df$a.q, my.df$b.q, sep=".")
    my.df$z <- c("1.1"=1, "1.2"=1, "1.3"=2, "1.4"=2, 
                 "2.1"=1, "2.2"=3, "2.3"=4, "2.4"=2,
                 "3.1"=5, "3.2"=6, "3.3"=7, "3.4"=8,
                 "4.1"=5, "4.2"=5, "4.3"=8, "4.4"=8)[my.df$a.b.q]
    

    This seems reasonable

    plot(my.df$a, my.df$b, col=my.df$z)
    

    With some data with more coverage:

    set.seed(1234)
    my.df <- data.frame(a=runif(1000, 1, 20), b=runif(1000, 61, 80))
    

    enter image description here

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

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have two tables with like below codes: Table: Accounts id | username |
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.