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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:47:39+00:00 2026-06-06T23:47:39+00:00

Lets say I have a dataset where rows keep falling out as I go

  • 0

Lets say I have a dataset where rows keep falling out as I go through the days, and I want to add these rows in again.

Example with missing rows:

Fruits <- c(rep(c("apples","oranges","pears","kiwis"),3),
    "bananas","oranges","pears","kiwis","bananas","pears","kiwis","bananas")
Days <- c(rep("Monday",4),rep("Tuesday",4),rep("Wednesday",5),
    rep("Thursday",4),rep("Friday",3))
Amounts <- c(10,15,20,20,10,15,20,20,10,15,20,20,25,15,20,20,25,20,20,25)
dfmissing <- data.frame(Fruits,Days,Amounts)

And I want it to fill new rows on Thursday and Friday when “apples” and “oranges” drop out as such.

Note that “bananas” appears for the first time on Wednesday, which complicates matters a little.

The completed table should look like the following

Fruits <- c(rep(c("apples","oranges","pears","kiwis"),2), 
    rep(c("apples","oranges","pears","kiwis","bananas"),3))
Days <- c(rep("Monday",4),rep("Tuesday",4),
    rep("Wednesday",5),rep("Thursday",5),rep("Friday",5))
Amounts <- c(rep(c("10","15","20","20"),2),rep(c("10","15","20","20","25"),3))
dfcomplete <- data.frame(Fruits,Days,Amounts)

Comparing the two tables

dfmissing
dfcomplete

Assume there is only one week of data so “Monday” never gets repeated etc – the column “Days” is a list of unique factors.

Thanks in advance.

  • 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-06T23:47:39+00:00Added an answer on June 6, 2026 at 11:47 pm

    Here id my quick slow for loop attempt. With what you described, I have used two if statements , one to check if there is new fruit to add, and one to check if there are any missing fruits. It isn’t very efficient, but it will get the job done. I will let you sort out the data.frame.

    # Get what days are in the data frame
    days <- unique(dfmissing$Days)
    # Start with the first day to get the fruits.
    fruit <- dfmissing[dfmissing$Days==days[1],"Fruits"]
    # Create a loop to loop over the actual days
    for(i in 2:length(days)){
        # Determine which fruits are present on this day.
        newdayfruit <- dfmissing[dfmissing$Days==days[i],"Fruits"]
    
        newFruitToAdd <- newdayfruit[is.na(match(newdayfruit,fruit))]
        # Check if there are any new fruits to add. 
        if(length(newFruitToAdd)>0){
            # Add the new fruit to the fruits.
            fruit <- c(as.character(fruit), as.character(newFruitToAdd))
        }
        # Check if there are any missing fruits.
        missingFruit <- fruit[is.na(match(fruit, dfmissing[dfmissing$Days==days[i],"Fruits"]))]
        # If there are missing fruits then the should be added to the dataframe
        if(length(missingFruit)>0){
            # Loop over each missing fruit.
            for(j in 1:length(missingFruit)){
                # Get the value of the missing fruit from the previous day
                updateWith <- dfmissing[dfmissing$Days==days[i-1]&dfmissing$Fruits==missingFruit[j],]
                # Change the day to the current day
                updateWith$Days <- days[i]
                # Add a row to the data frame with the updated value.
                dfmissing <- rbind(dfmissing, updateWith)
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two data tables in a data set for example lets say Servers:
Lets say I have a dataset about carrot yield from different fields and different
So lets say I have a dataset based on: SELECT TopicLink.*, Topic.Name AS FromTopicName,
Scenario Lets say for example I have a series of events that fire continually
Lets say that I have this records in a dataset: name1 XXXXX XXXXXX name1
let's say I have public delegate DataSet AutoCompleteDelegate( string filter, long rowOffset); can I
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say we have a table here, populated with the following data: acc_id1 acc_id2
Lets say I have five tables named table1, table2 ... table5. I have already
Lets Say i have a table like this WEB_LIST_TABLE KEY Value ---------------------------------------- 134 google.com

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.