I'm working in a loop and generating a df on each iteration. As I iterate, I am joining the results into one big table. The following code works as intended, but seems overly complicated. Is there a way to simplify this so I don't have to have an if/else block?
if(exists("ModelOutput.Full")){
ModelOutput.Full <- ModelOutput.Full%>%
distinct()%>%
left_join(ModelOutput, by = "ID")
} else {
ModelOutput.Full <- ModelOutput
}
I was hoping to just use the else code and have it create ModelOutput.Full on the first iteration, but that doesn't happen.
Also, feel free to suggest other optimizations that I'm not asking about. I'm sure they exist.
Edit 2: Thanks to DSGym's input, I've gotten this working, though it took slight modification of their answer, as I didn't provide reproducible code in my initial question. Here's an illustration of what worked for me:
regions <- c(1:7)
drivers <- c(1:5)
ModelOutput <- list()
ModelOutput.Regional <- list()
ID <- c(1:6961896)%>%
as.vector()%>%
as.data.frame()%>%
rename("ID"=".")
modelOutput <- list()
modelOutput.regional <- list()
for (region in regions) {
for (driver in drivers)
vals <- sample(0:10, 6961896, replace = TRUE)/10
outName <- paste("driver",driver,sep="")
vals <- vals%>%
as.vector()%>%
as.data.frame()%>%
rename(!!outName := ".")%>%
bind_cols(ID)
ModelOutput[[driver]] <- vals
}
ModelOutput.Regional[[region]] <- as.data.frame(Reduce(function(x, y) merge(x, y, by = "ID", all.x = TRUE), ModelOutput))
}
ModelOutput.Full <- Reduce(function(x, y) bind_rows(x, y), ModelOutput.Regional)
This generates my desired output of a giant data frame with all the regional data and the scores of each 'driver' in labeled columns like this:
ID driver1 driver2 driver3 driver4 driver5
1 0.1 0.2 0.4 0.6 0.4
2 0.4 0.6 0.5 0.7 0.7
3 0.3 0.7 0.5 0.2 0.3