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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:47:39+00:00 2026-05-28T17:47:39+00:00

In the following matrix dataset: 1 2 3 4 5 1950 7 20 21

  • 0

In the following matrix dataset:

       1  2   3   4   5  
1950   7 20  21  15  61  
1951   2 10   6  26  57  
1952  12 27  43  37  34  
1953  14 16  40  47  94  
1954   2 17  62 113 101  
1955   3  4  43  99 148  
1956   2 47  31  85  79  
1957  17  5  38 216 228  
1958  11 20  15  76  68  
1959  16 20  43  30 226  
1960   9 28  28  70 201  
1961   1 31 124  74 137  
1962  12 25  37  41 200  

I have been trying to calculate colSums by decade i.e., find sum the each column from 1950-1959 and then from 1960-69 and so on.

I tried tapply, ddply, etc but couldn’t figure out something that would actually work.

  • 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-28T17:47:40+00:00Added an answer on May 28, 2026 at 5:47 pm

    First we set up the matrix used as input.

    Lines <- "1  2   3   4   5  
    1950   7 20  21  15  61  
    1951   2 10   6  26  57  
    1952  12 27  43  37  34  
    1953  14 16  40  47  94  
    1954   2 17  62 113 101  
    1955   3  4  43  99 148  
    1956   2 47  31  85  79  
    1957  17  5  38 216 228  
    1958  11 20  15  76  68  
    1959  16 20  43  30 226  
    1960   9 28  28  70 201  
    1961   1 31 124  74 137  
    1962  12 25  37  41 200  "
    DF <- read.table(text = Lines, check.names = FALSE)
    m <- as.matrix(DF)
    

    Now, below, we show some alternative solutions. (1) seems the most flexible in that we can easily replace sum with other functions to get different effects but (2) is the shortest for this particular problem. Also note that there are some slight differences. (1) produces a data.frame while the other two produce a matrix.

    1) aggregate

    decade <- 10 * as.numeric(rownames(m)) %/% 10
    m.ag <- aggregate(m, data.frame(decade), sum)
    

    which gives this data.frame:

    > m.ag
      decade  1   2   3   4    5
    1   1950 86 186 342 744 1096
    2   1960 22  84 189 185  538
    

    2) rowsum This one is shorter. It produces a matrix result.

    rowsum(m, decade)
    

    3) split/sapply. This one produces a matrix as well. if we had DF we could replace as.data.frame(m) with DF shortening it slightly.

    t(sapply(split(as.data.frame(m), decade), colSums))
    

    EDIT: added solutions (2) and (3) Added some clarifications.

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

Sidebar

Related Questions

Say I have the following matrix: A = randi(10, [6 3]) 7 10 3
In R I have the following matrix (each row represents a bootstrap 95% confidence
Is there a easy way how to produce following matrix: a = 4 5
I have this following numpy matrix that I want to sort in ascending order
Using the following simulated time series: n=70 m1 = matrix(rnorm(n), ncol=7) m2 = matrix(rnorm(n,
I have several blocks of the following code that each use there own matrix.
I'm using scipy.linalg to solve a matrix equation A*x = b The following code
I tried to update some part of a matrix, I got the following error
How can I create a const boost matrix? The following did not work: const
I have the following class: template <typename T> class matrix { private: int _n;

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.