Reputation: 881
I have a function tot_prof(number, df1, number, df2)
where the output is itself another df.
I am trying to iterate through df3
to get the string
values for the tot_prof
function.
Each iteration should store the results of tot_prof
as a new df (or all the results can be combine).
How can I do this?
I tried to create another function like so:
iterator <- function(df, cluster, prod_dat, q_dat) {
l <- list(length(df))
iter <- 1
for (i in l){
thisl <- i
for (n in 1:nrow(df)) {
prod <- n
iter_out <- tot_prof(cluster, prod_dat, prod, q_dat)
l[[iter]] <- data.frame(iter_out)
}
iter <- iter + 1
}
l <- as.data.frame(l)
return(l)
}
It should return 310 observations for 10 different prod
values - instead it returns 61 observations of the same prod
.
I am very confused. Please help.
Upvotes: 0
Views: 857
Reputation: 211
Sorry for not commenting (I am not allowed) but maybe a solution here:
For each prod
you're saving a new data frame over the last one. A solution would be combining all prod
data frame by columns (cbind()
) or rows (rbind()
). And at the end save it to the list l
.
iterator <- function(df, cluster, prod_dat, q_dat) {
l <- list(length(df))
for (i in l){
iter <- i
for (n in 1:nrow(df)) {
prod <- n
df <- tot_prof(cluster, prod_dat, prod, q_dat)
if(prod == 1) {
iter_out <- df
}else {
iter_out <- cbind(iter_out, df) # or rbind depending what you want
}
}
l[[iter]] <- iter_out
iter <- iter + 1
}
return(l)
}
Upvotes: 1