[英]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
). 我正在设计一个仿真,该仿真使用来自两个单独数据帧( df
和year
)的输入来迭代地运行代码块。 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
). 只需将对象保存在一个命名列表中(如果它们最初位于带有split
或by
一个数据框中,则可以创建该列表)。 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.