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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:37:29+00:00 2026-05-30T09:37:29+00:00

Let’s say I have this vector v <- c(1:100) And I want to get

  • 0

Let’s say I have this vector

v <- c(1:100) 

And I want to get this:

b[1] = sum (v[c(1:10)])
b[2] = sum (v[c(11:20)])
...
...

I can do a loop to solve this, but I am pretty sure there is a “R way” that should be something like:

b <- groupedSum(v, 10) 

where b will be a vector which will have each group of 10 summed
What is the R way?

  • 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-30T09:37:30+00:00Added an answer on May 30, 2026 at 9:37 am
    > tapply( v, (seq_along(v)-1) %/% 10, sum)
      0   1   2   3   4   5   6   7   8   9 
     55 155 255 355 455 555 655 755 855 955
    

    If there were NA’s in there you might need to add na.rm=TRUE to the argument list after sum.

    Comments: I think Tyler’s approach is more complete because it provided better documentation. It suffers from needing to work around the vagaries of the cut() function which I have always felt had the wrong defaults. In order to create a grouping that captures all of 1:100 he needs to use a 101 element vector. But that’s not Tyler’s fault. Send him any further votes, his answer is better.

    If gsk can use by-objects without running into class difficulties, he’s a better man than I. The output looks like a list but it’s really something different. Using his example:

    > is.list(by(v,idx,sum))
    [1] FALSE
    > is.matrix(by(v,idx,sum))
    [1] FALSE
    > is.vector(by(v,idx,sum))
    [1] FALSE
    

    I think by-objects are sort of like named vectors and sort of like matrices but the failure to inherit matrix class has always confused the heck out of me.

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

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I can call a method like this: core::get() . What is the
Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say I have thousands of users and I want to make the passwords
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have this code: <p dataname=description> Hello this is a description. <a

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.