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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:38:18+00:00 2026-06-08T23:38:18+00:00

I have a matrix with three columns: county, date, and number of ED visits.

  • 0

I have a matrix with three columns: county, date, and number of ED visits. The dates repeat for each county, like this (just an example):

  County A  1/1/2012  2
  County A  1/2/2012  0
  County A  1/3/2012  5
  ... etc.
  County B  1/1/2012  3
  County B  1/2/2012  4
  ... etc.

I would like to collapse this matrix to sum the visits from all counties for each date. So it would look like this:

1/1/2012  5
1/2/2012  4
etc.

I am trying to use the "table()" function in R but can’t seem to get it to operate on visits by date in this manner. When I do "table(dt$date, dt$Visits)" it gives me a table of frequencies like this:

             0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  2011-01-01 3 1 2 0 1 1 0 2 0 0  0  0  0  0  0  0
  2011-01-02 2 3 1 0 0 1 0 0 1 0  2  0  0  0  0  0
  2011-01-03 3 1 1 2 1 0 0 0 0 1  0  0  0  0  1  0

Any suggestions? Is there a better function to use, perhaps a “sum” of some sort?

Thanks!

  • 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-06-08T23:38:20+00:00Added an answer on June 8, 2026 at 11:38 pm

    As @DWin states, table() is not for summation, but for record counts.

    I give three examples of approaches, using plyr, data.table and aggregate

    all_data <- expand.grid(country = paste('Country', LETTERS[1:3]), 
      date = seq(as.Date('2012/01/01'), as.Date('2012/12/31'), by = 1) )
    
    all_data[['ed_visits']] <- rpois(nrow(all_data), lambda = 5)
    
    
    
    # using plyr
    
    library(plyr)
    
    by_date_plyr <- ddply(all_data, .(date), summarize, visits = sum(ed_visits))
    
    
    # using data.table
    library(data.table)
    all_DT <- data.table(all_data)
    by_date_dt <- all_DT[, list(visits = sum(ed_visits)), by = 'date' ]
    
    # using aggregate
    by_date_base <- aggregate(ed_visits ~ date, data = all_data, sum)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large number of entries arranged in three columns. Sample of the
I am currently have a nx3 matrix array. I want plot the three columns
In Javascript, I have a matrix with a variable number of rows and columns,
I have a matrix that has 20 rows and 51 columns. I would like
I have a data frame with three columns and I'd like to make a
I have a string matrix with N rows and 2 columns. Each cell stores
I have a matrix of character/number combinations, and I need to delete those columns
I have matrix (a) with (1:10),<10 x 1> double. I would like to copy
I have a matrix of data (the columns represent time, and the rows spectrum
I have a matrix of objects that contains data in this form: name A,2,name

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.