简体   繁体   English

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

[英]Call new data frame for each iteration: Loop through data frames?

I am designing a simulation that iteratively runs a block of code using inputs from two separate data frames ( df and year ). 我正在设计一个仿真,该仿真使用来自两个单独数据帧( dfyear )的输入来迭代地运行代码块。 The resulting data frame is a modified version of df , which is then saved on my hard-drive under two separate file names: one that is permanently stored for future analysis, and another that is recalled for the next iteration. 生成的数据帧是df的修改版本,然后以两个单独的文件名保存在我的硬盘驱动器上:一个永久存储以供将来分析,另一个用于下一次迭代调用。

Here is my problem: The data frame year needs to be a completely new data frame for each iteration (ie, the following year's data). 这是我的问题:数据框year必须是每次迭代的全新数据框(即,下一年的数据)。

Could this be accomplished with something like a for loop, where the index [i] is the next year's data frame (rather than a row within a data frame, which is how I understand for loops to operate)? 是否可以通过for循环来实现,其中索引[i]是下一年的数据帧(而不是数据帧中的一行,这是我对循环操作的理解)? I suspect the answer involves a list? 我怀疑答案涉及清单? Here are some dummy data attempting to demonstrate the issue: 以下是一些尝试证明此问题的伪数据:

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

Currently, I manually reload df and reset the year for each iteration (eg, I would reassign year using year1991 for the second iteration in this example), but I'm certain there's a better way to automate the entire process. 当前,我手动重新加载df并为每次迭代重置year (例如,在此示例中,我将使用year1991重新分配year来进行第二次迭代),但是我敢肯定有一种更好的方法可以使整个过程自动化。 Thank you! 谢谢!

Simply save objects in a named list (which can be created if they originally were in one data frame with split or by ). 只需将对象保存在一个命名列表中(如果它们最初位于带有splitby一个数据框中,则可以创建该列表)。 Then loop elementwise with Map (wrapper to mapply ) through list's names and objects through a defined process for looping 然后通过定义的循环过程,使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R数据帧中的列之间循环并在每次迭代中使用列名创建新的数据帧? - How to loop through the columns in an R data frame and create a new data frame using the column name in each iteration? for循环浏览数据帧列表并将结果保存在新数据帧中 - for loop through a list of data frames and save results in new data frame 如何在每次迭代中使用行名称遍历 R 数据帧和查看/加载数据帧中的列? - How to loop through the columns in an R data frame and View/ Load data frame using the Row name in each iteration? 将 for 循环中的每次迭代保存为数据框 - Saving each iteration in a for loop as a data frame 如何创建名称基于 R 中 For 循环的每次迭代的新数据框 - How to Create New Data Frames Whose Names Are Based On Each Iteration Of A For Loop in R 如何遍历数据框列表并将名称的数字部分分配为数据框的新列值? - How to loop through a list of data frames and assign the numeric portion of the name as a new column value for the data frame? 想要通过在循环中子集一个数据帧并基于i值分配每个数据帧名称来在R中创建新的数据帧 - Want to create new data frames in R by subsetting a data frame in a loop and assign each data frame name based on i value 循环遍历数据框以生成新列 - Loop through data frames to produce new column 用于循环使用数据框列表并使用变量名从每个迭代创建新的数据框 - For Loop Over List of Data Frames and Create New Data Frames from Every Iteration Using Variable Name 当每次迭代的输出是 data.frame 时循环 - Loop when the output for each iteration is a data.frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM