每次迭代都调用新的数据框:遍历数据框?

时间:2018-09-20 18:43:56

标签: r loops recursion iteration simulation

我正在设计一个仿真,该仿真使用来自两个独立数据帧(dfyear)的输入来迭代地运行代码块。生成的数据框是df的修改版本,然后以两个单独的文件名保存在我的硬盘上:一个文件名永久存储以供将来分析,另一个文件名供下次迭代使用。

这是我的问题:year数据框必须是每次迭代的全新数据框(即,下一年的数据)。

这可以通过for循环来实现,其中索引[i]是下一年的数据帧(而不是数据帧中的一行,这是我对循环操作的理解)?我怀疑答案涉及清单?以下是一些虚拟数据,试图证明这个问题:

df <- tibble(
  x = 1:25,
  y = rnorm(25, 22, 8))

year1990 <- tibble(
  Year = 1990, 
  DayOfYear = 1:6, 
  temp = seq(0, 20, 4))

year1991 <- tibble(
  Year = 1991, 
  DayOfYear = 1:6, 
  temp = seq(0, 25, 5))

year1992 <- tibble(
  Year = 1992, 
  DayOfYear = 1:6, 
  temp = seq(0, 15, 3))

#### Beginning of Code to Be Repeated ####

year <- year1990         # Start with this year, BUT each subsequent iteration needs the following year's data
df$survive <- ifelse(max(year$temp) <= df$y, "Dead", "Live")
write.csv(df, "location/f.csv",row.names=FALSE)  # Write temporary CSV to be recalled
write.csv(df, paste(year[1,1], ".csv", sep = ""), row.names = FALSE)      # Write permanent CSV for storage

#### End of Code to Be Repeated ####

# Reload the newly modified data frame
setwd()
df <- read.csv("df.csv")  

当前,我为每次迭代手动重新加载df并重置year(例如,在此示例中,我将使用year为第二次迭代重新分配year1991),但我敢肯定,有一种更好的方法可以使整个过程自动化。谢谢!

1 个答案:

答案 0 :(得分:1)

只需将对象保存在一个命名列表中(如果它们最初位于一个带有splitby的数据框中,则可以创建该列表)。然后通过已定义的循环过程,通过列表的名称和对象与Map(包装到mapply)进行元素循环

year_list <- list(
  year1990 = tibble(Year = 1990, DayOfYear = 1:6, temp = seq(0, 20, 4)),    
  year1991 = tibble(Year = 1991, DayOfYear = 1:6, temp = seq(0, 25, 5)),    
  year1992 = tibble(Year = 1992, DayOfYear = 1:6, temp = seq(0, 15, 3))
)

proc <- function(n, d) {    
    year <- d        
    df$survive <- ifelse(max(year$temp) <= df$y, "Dead", "Live")
    write.csv(df, "location/f.csv", row.names = FALSE)  

    # Write temporary CSV to be recalled
    write.csv(df, paste0(n, ".csv"), row.names = FALSE)  

    return(df)
}

# ITERATIVELY SAVES CSVs AND RETURNS dfs WITH UPDATED survive COLUMN
output_list <- Map(proc, names(year_list), year_list)