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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:38:11+00:00 2026-05-28T03:38:11+00:00

I have a data frame df with 2 variables A and B. I would

  • 0

I have a data frame df with 2 variables A and B. I would like to split A in groups 1 and 2 so that mean(df$B[df$group==1]) as close as possible to mean(df$B[df$group==2])

Or just to express it otherwise, what I would like is to find a cut point (cutp) in df$A that would minimize the abs(mean(df$B[df$A<cutp])-mean(df$B[df$A>=cutp]))

Any ideas?

  • 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-28T03:38:12+00:00Added an answer on May 28, 2026 at 3:38 am

    If you want to find a threshold on variable A, to split the data into two groups, so that the means of B in those two groups be similar, you can compute these means for all possible cut-points, and check when the distance between those means is minimal.

    # Sample data
    n <- 10
    d <- data.frame(
      A = rnorm(n),
      B = rnorm(n)
    )
    
    # The quantity to minimize
    # (You can use a loop instead of apply.)
    d$differences <- apply(
      d, 1, 
      # Compute the difference of the means for each value of A
      function (u) { 
        i <- d$A <= u[1]; 
        abs( mean( d$B[which(i)]) - mean(d$B[which(!i)] ) )
      } 
    )
    # The mean of an empty vector is NaN: discard those values
    d$differences[ ! is.finite( d$differences ) ] <- Inf
    # Take the minimum
    threshold <- d$A[ which.min( d$differences ) ]
    # Build the groups
    d$group <- ifelse( d$A <= threshold, "group 1", "group 2" )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
I have three variables in a data frame and would like to swap the
I have a data.frame in R that looks like this: score rms template aln_id
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame with gaps like this: Var1 Var2 Var3 1 NA
I have a data frame in R that has come about from running some
I have a large data.frame, and I'd like to be able to reduce it
I have 13 quantitative variables in a data.frame (called 'UNCA'). The variables are named
I have a df/zoo/xts/whatever that is split by day of week. I'd like to
I have a large data frame with date variables, which reflect first day of

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.