I have a script of a large data set (big j at loop for). I have already deleted some cycles but I think it can be optimized again. I tried the foreach() function but it gives me some errors. I do not know how to parallelize and if it is necessary. the script takes the dataframe par [] as input and for each record (which contains some parameters for the calculations) it must create a new record: parpa[]. It must first make calculations and for this the DF2 data frame is built inside the cycle. Finally the values contained in perpa[] are calculated through a while loop. First question: Does anyone have an idea to optimize yet? I used mapply() but it returns errors.
par <- data.frame(CA=runif(n = 50, min = 70000, max = 100000),
D=round(runif(n = 50, min = 70, max = 90),0),
P=runif(n = 50, min = 900, max = 20000),
A=round(runif(n = 50, min = 50, max = 70),0))
parpa <- data.frame(matrix(nrow = nrow(par), ncol = 3*V))
comp <- function(CA, D, P, A){
vect <- rep('numeric', 3*V)
b <- 1
k <- 1
while (((b+1) <= (D+1))&(k < V)) {
a <- b+1
b <- min((a+8-1), (D+1))
vect[c(1+4*k, 2+4*k, 3+4*k, 4+4*k)] <- c(mean(DF2$Z[a:b]), sum(DF2$X[a:b]),
mean(DF2$Q[a:b]), sum(DF2$AE[a:b]))
k <- k+1
}
return(vect)
}
#loop
for (j in 1:nrow(par)) {
CA <- par$CA[j]
D <- par$D[j]
R <- 0.01*D
P <- par$P[j]
A <- par$A[j]
COST <- 500
V <- 5
#DF2
DF2 <- data.frame(M=0:D)
O <- function(x) {
c <- COST*D*DF2$M/R
return(c)
}
DF2$O <- O(D)
DF2$E <- (D*DF2$M+2)/D*(D+4)
DF2$Q <- (CA-DF2$M)*D
DF2$X <- (CA-DF2$O)*(DF2$E+P)
Func <- function(x) {return(round(x/30, 2))}
DF2$Z[(A+2):(D+1)] <- unlist(sapply(DF2$E[(A+2):(D+1)], Func))
parpa[j,] <- comp(CA, D, P, A)
}
-----------------------------with mapply()-----------------------------------
#loop
outputpa <- function(CA, D, P, A) {
CA <- par$CA
D <- par$D
R <- 0.01*D
P <- par$P
A <- par$A
COST <- 500
V <- 5
#DF2
DF2 <- data.frame(M=0:D)
O <- function(x) {
c <- COST*D*DF2$M/R
return(c)
}
DF2$O <- O(D)
DF2$E <- (D*DF2$M+2)/D*(D+4)
DF2$Q <- (CA-DF2$M)*D
DF2$X <- (CA-DF2$O)*(DF2$E+P)
Func <- function(x) {return(round(x/30, 2))}
DF2$Z[(A+2):(D+1)] <- unlist(sapply(DF2$E[(A+2):(D+1)], Func))
}
parpa <- mapply(outputpa, par$CA, par$D, par$P, par$A)