R代码-添加行的巧妙循环
假设我有一个数据集,其中的行随着时间的推移不断减少,我想再次添加这些行 缺少行的示例:R代码-添加行的巧妙循环,r,loops,R,Loops,假设我有一个数据集,其中的行随着时间的推移不断减少,我想再次添加这些行 缺少行的示例: 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),
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)
假设只有一周的数据,所以“星期一”永远不会重复,等等,“天”列是一个独特因素的列表
提前感谢。这里是我的快速慢速循环尝试。根据您的描述,我使用了两个if语句,一个用于检查是否有新的水果要添加,另一个用于检查是否有任何缺少的水果。这不是很有效率,但它能完成工作。我会让你整理一下数据框
# 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)
}
}
}
#获取数据框中的天数
您想用什么填充缺少的行NA
或某个值?行的最后一个值(如dfcomplete中的值)
dfmissing
dfcomplete
# 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)
}
}
}