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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:33:11+00:00 2026-05-26T02:33:11+00:00

I have a data set that includes cases by year and month. Some months

  • 0

I have a data set that includes cases by year and month. Some months are missing, and I’d like to create rows with a case count of zero for those months.

Here is an example, and my current brute force approach. Thanks for any pointers. Obviously, I’m new at this.

# fake data
library(plyr)
rm(FakeData)
FakeData <- data.frame(DischargeYear=c(rep(2010, 7), rep(2011,7)),
                       DischargeMonth=c(1:7, 3:9),
                       Cases=trunc(rnorm(14, mean=100, sd=20)))

# FakeData is missing data for some year/months
FakeData

# Brute force attempt to add rows with 0 and then total 
for(i in 1:12){
  for(j in 1:length(unique(FakeData$DischargeYear))){
    FakeData <- rbind(FakeData, data.frame(
                DischargeYear=unique(FakeData$DischargeYear)[j],
                DischargeMonth=i,
                Cases=0))
      }
     }

FakeData <- ddply(FakeData, c("DischargeYear","DischargeMonth"), summarise, Cases=sum(Cases))

# FakeData now has every year/month represented
FakeData
  • 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-26T02:33:11+00:00Added an answer on May 26, 2026 at 2:33 am

    Using your FakeData data frame, try this:

    # Create all combinations of months and years
    allMonths <- expand.grid(DischargeMonth=1:12, DischargeYear=2010:2011)
    # Keep all month-year combinations (all.x=TRUE) and add in 'Cases' from FakeData
    allData <- merge(allMonths, FakeData, all.x=TRUE)
    # 'allData' contains 'NA' for missing values. Set them to 0.
    allData[is.na(allData)] <- 0
    # Print results
    allData
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up a Core Data model that includes an entity, Item with
I have a data set that looks like this: 000 100 200 300 010
I have a data set that is around 700 rows with eight columns of
I have a data set that looks like the following. EMPLID PHONE_TYPE PHONE ------
I have a data set that is organized in the following manner: Timestamp|A0001|A0002|A0003|A0004|B0001|B0002|B0003|B0004 ...
I have the following data set that I am trying to plot with ggplot2,
I have a very large possible data set that I am trying to visualize
We have this set of data that we need to get the average of
I have a set of data that models a hierarchy of categories. A root
I have a set of data that needs to be displayed as a crosstab

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.