R代码-聪明的循环添加行 [英] R code - clever loop to add rows

查看:67
本文介绍了R代码-聪明的循环添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个数据集,随着时间的流逝,行不断掉落,我想再次添加这些行.

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.

缺少行的示例:

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.

完成的表格应如下所示

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)

比较两个表

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.

谢谢.

推荐答案

此处是我尝试循环的速度很慢的地方.根据您的描述,我使用了两个if语句,一个用于检查是否要添加新的水果,另一个用于检查是否缺少任何水果.它不是很有效,但是可以完成工作.我将让您整理出data.frame.

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)
        }
    }
}

这篇关于R代码-聪明的循环添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆