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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:47:53+00:00 2026-05-23T12:47:53+00:00

Using R, what is the best way I can aggregate rows on a condition

  • 0

Using R, what is the best way I can aggregate rows on a condition that spans multiple rows.
For example to aggregate any rows where z = 0 for n or more times.

What this would look like run on the following sample table with n = 3.

Sample Table x:

x   y   z
0   0   6
5   5   0
40  2   0
4   0   0
10  0   1
0   0   2
11  7   0
0   4   0
0   0   0
0   0   0
0   0   2
18  0   4

Results Table:

x   y   z
0   0   6
49  7   0 <- Above two rows got aggregated
10  0   1
0   0   2
11  11  0 <- Above three rows got aggregated
0   0   2
18  0   4
  • 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-23T12:47:53+00:00Added an answer on May 23, 2026 at 12:47 pm

    This is the code I used to produce your result. If you have any questions, fire away.

    mmf <- read.table(textConnection("x   y   z # read in your example data
    0   0   6
    5   5   0
    40  2   0
    4   0   0
    10  0   1
    0   0   2
    11  7   0
    0   4   0
    0   0   0
    0   0   0
    0   0   2
    18  0   4"), header = TRUE)
    
    # see where there are zeros in the y column
    mmf.rle <- rle(mmf$z) 
    mmf.rle <- data.frame(lengths = mmf.rle$lengths, values = mmf.rle$values)
    
    merge.rows <- 3
    # select rows that have more or equal to three zeros
    mmf.zero <- which(mmf.rle$values == 0 & mmf.rle$lengths >= merge.rows)
    
    for (i in mmf.zero) {
    # find which positions are zero, calculate sums and insert the result into a data.frame where the rows in question were turned to NA
        m.mmf <- mmf.rle$lengths[1:i] # select elements from 1 to where the zero appears
        select.rows <- (sum(m.mmf[1:length(m.mmf) - 1])+1):sum(m.mmf) # magic
        mmf.sum <- colSums(mmf[select.rows, ]) # sum values column-wise for rows that have at least three zeros in z
        mmf[select.rows,] <- NA # now that we have a sum by columns, we turn those numbers into NAs...
        mmf[select.rows[1], ] <- mmf.sum # ... and insert summed result into the first NA row       
    }
    
    # remove any left over NA rows
    mmf <- mmf[complete.cases(mmf),]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Prism - what is the best way to handle navigation in a WPF
Using C# 2.0 what is the best way to implement dynamic form controls? I
Whats the best way to zip up files using C#? Ideally I want to
what is best way to send email from PHP using server authantication. Userid &
what is the best way of displaying/using the revision number in a java webapp?
Whats the best way to get the current time (HH:MM:SS) using Objective-C. I would
What is the best way to interact with a database using Haskell? I'm accustomed
What is the best way to reset a PIC18 using C code with the
What's the best way to get the last inserted id using sqlite from Java?
What might be the best way to start programming using boost lambda libraries.

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.