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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:56:33+00:00 2026-05-22T16:56:33+00:00

I am trying to split a data frame based on a boundary value for

  • 0

I am trying to split a data frame based on a boundary value for a given variable, compute something on both sides of the boundary and output a matrix (preferably a data frame). Example code bellow:

set.seed(1)
tdata <- data.frame(a1=rnorm(100, mean=5, sd=2), a2=rep(0:1, length.out=100))
tall <- sapply(1:9, function(x) {
  d <- split(tdata, tdata$a1 <= x)
  sapply(d, function (y) {
    1 - max(table(y$a2)/nrow(y))
  })
})

My result:

> allErr
           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
FALSE 0.4949495 0.4895833 0.4943820 0.4933333 0.4444444 0.4411765 0.3333333
TRUE  0.0000000 0.2500000 0.4545455 0.4800000 0.4347826 0.4696970 0.4705882
      [,8] [,9]
FALSE  0.5  0.5
TRUE   0.5  0.5

My continuous variable is tdata$a1 and I want to split the data frame every time in 2 using a boundary value from 1:9, perform a computation on a2 for each part of the split, and return that back.

My question here: what is the best way to do this in terms of elegance (looked at a plyr solution but can’t avoid using the first sapply) and more importantly correct usage of other R functions I might not be aware of. I’m also afraid that my solution will not scale that well with much larger data frames than the ones I currently have (~10000 rows).

  • 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-22T16:56:34+00:00Added an answer on May 22, 2026 at 4:56 pm

    Nothing more elegant is springing to mind, but this modification may help your solution scale slightly better by splitting an index vector rather than the entire data frame:

    set.seed(1)
    tdata <- data.frame(a1=rnorm(100, mean=5, sd=2), a2=rep(0:1, length.out=100))
    tall <- sapply(1:9, function(x) {
             d <- split(seq_along(tdata$a2), tdata$a1 <= x)
             sapply(d, function (y) {
              1 - max(table(tdata$a2[y])/length(y))
             })
            })
    

    The performance gain for this toy example is fairly small, most likely because your data frame only has two columns. If your real data frame has more columns, you’ll see more of a benefit from splitting an index vector.

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

Sidebar

Related Questions

I'm trying to add a Row to my data.frame to spit out the average
a.2<-sample(1:10,100,replace=T) b.2<-sample(1:100,100,replace=T) a.3<-data.frame(a.2,b.2) r<-sapply(split(a.3,a.2),function(x) which.max(x$b.2)) a.3[r,] returns the list index, not the index for
I'm trying to find the best way to split data into groups, for example
I am trying to split string data using Python which is delimited by the
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I'm trying to split a string up into words and punctuation, adding the punctuation
I'm trying to split an app.config file into multiple files to make it easier
I'm trying to split up a web site into two sections. One which should
I'm trying to split a string on its punctuation, but the string may contain
I am trying to split a string in Javascript using a Regular Expression. My

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.