Updating a matrix using data from a forced matrix
Here is the code I am working with:
y <- c(0,5,10)
n <- 9
R <- t(c(2.05, 2.05, 2.05, 2.55, 2.55, 2.55, 2.95, 2.95, 2.95))
R <- (replicate(3,R))
R <- (matrix(R, nrow=3))
R <- t(apply(R,1,sort))
mat <- t(sapply(y, function(test) pmax(seq(test, (test-n+1), -1), 0) ))
mat
P <- replicate(ncol(R),(c(6447.88,6447.88,6447.88)))
EnvTest <- new.env()
EnvTest$Orig <- 548453.5
FuncTest <- function(pp){
EnvTest$Orig <- EnvTest$Orig-(P[,pp]-EnvTest$Orig*R[,pp]/1200)
return(EnvTest$Orig)
}
Test<- rbind(EnvTest$Orig,
do.call(rbind,lapply(1:9, FuncTest)))
x <- t(Test)
x
This gives:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 548453.5 542942.6 537422.2 531892.4 526574.8 521245.9 515905.7 510726.1 505533.7 500328.6
[2,] 548453.5 542942.6 537422.2 531892.4 526574.8 521245.9 515905.7 510726.1 505533.7 500328.6
[3,] 548453.5 542942.6 537422.2 531892.4 526574.8 521245.9 515905.7 510726.1 505533.7 500328.6
This is what I tell the function to do to make it right. But now I want to use some information from the matrix R*(mat==0)
which is:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 2.05 2.05 2.05 2.55 2.55 2.55 2.95 2.95 2.95
[2,] 0.00 0.00 0.00 0.00 0.00 2.55 2.95 2.95 2.95
[3,] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
So, I tried to do something like this ...
FuncTest2 <- function(pp){
EnvTest$Orig <- ifelse(R*(mat==0)==0,EnvTest$Orig,EnvTest$Orig-(P[,pp]-EnvTest$Orig*R[,pp]/1200))
return(EnvTest$Orig)
}
Test2<- rbind(EnvTest$Orig,
do.call(rbind,lapply(1:9, FuncTest2)))
x2 <- t(Test2)
x2
But this did not lead to the desired result:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 548453.5 542942.6 537422.2 531892.4 526574.8 521245.9 515905.7 510726.1 505533.7 500328.6
[2,] 548453.5 548453.5 548453.5 548453.5 548453.5 543171.1 538058.5 532933.3 527795.5 522645.1
[3,] 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5
So, basically, if R * (mat == 0) = 0, then it EnvTest$Orig <- 548453.5
doesn't change until R*(mat==0)
it is greater than zero, but EnvTest$Orig
updates.
Another try
I also tried the following, but it didn't work, but I think something along these lines will prove fruitful.
maybe <- R*(mat==0)
FuncTest2 <- function(pp, maybe){
EnvTest$Orig <- if(maybe == 0) (EnvTest$Orig-0) else
(EnvTest$Orig-(P[,pp]-EnvTest$Orig*R[,pp]/1200))
return(EnvTest$Orig)
}
Test2<- rbind(EnvTest$Orig,
do.call(rbind,lapply(1:9, FuncTest2)))
x2 <- t(Test2)
x2
source to share
First, with respect to your calculations, FuncTest()
and FuncTest2()
, R
is a constant, since it does not change during these function calls or when processing the surrounding data (referring to do.call()
/ rbind()
/ t()
stuff). This is R
:
R;
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,] 2.05 2.05 2.05 2.55 2.55 2.55 2.95 2.95 2.95
## [2,] 2.05 2.05 2.05 2.55 2.55 2.55 2.95 2.95 2.95
## [3,] 2.05 2.05 2.05 2.55 2.55 2.55 2.95 2.95 2.95
As you can see, none of its elements are zero. This means that the expression
R*(mat==0)==0
equivalent to
mat!=0
since multiplication by R
cannot turn a non-zero value into zero or turn zero into a non-zero value.
If you still want to parameterize the condition ifelse()
on R
, perhaps because you can run this code for different values R
that can contain zeros, then it should be rewritten as
R==0|mat!=0
Second, your code has a little suspicious pattern regarding the dimension of the state variable EnvTest$Orig
. You initialize it with a singleton vector (548453.5), but the first time you run FuncTest()
it, it is immediately overwritten by a three-element vector, where the initial scalar is looped 3 times to place the three-element vectors P[,pp]
and R[,pp]
. From that moment on, it is constantly a three-element vector. The FuncTest2()
(first try), as mat
and R
are matrices of 3x9, true and false vector t arguments ifelse()
are cyclically cover the dimension, and then EnvTest$Orig
overwrites the obtained matrix 3x9. From that moment on, it is permanently a 3x9 matrix.
You should probably think carefully about what dimension you want for EnvTest$Orig
and stick to it from start to finish. Judging by your expected output, it looks like you want it to be a vector of length 3, and so you must index the matrix in a ifelse()
conditional:
FuncTest2 <- function(pp) {
EnvTest$Orig <- ifelse(R[,pp]==0|mat[,pp]!=0,EnvTest$Orig,EnvTest$Orig-(P[,pp]-EnvTest$Orig*R[,pp]/1200));
return(EnvTest$Orig);
};
EnvTest$Orig <- 548453.5;
Test2 <- rbind(EnvTest$Orig,do.call(rbind,lapply(1:9,FuncTest2)));
x2 <- t(Test2);
x2;
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 548453.5 542942.6 537422.2 531892.4 526574.8 521245.9 515905.7 510726.1 505533.7 500328.6
## [2,] 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 543171.1 538058.5 532933.3 527795.6
## [3,] 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5 548453.5
However, this result does not match your expected result on the second line and I am not sure why.
source to share