Adam Warner
Adam Warner

Reputation: 1354

Updating Issue Using Function in R

I am having an updating issue with my code and was hoping that somebody more versed with "functions" in R will be able to figure it out. As you see here the variable "R" changes, and when calling upon that variable in FuncTest the only the R value of 2.05 is used. Which is why the first 3 values produce the desired output then it all goes down hill from there. How would this be amended?

 R <- t(c(2.05, 2.05, 2.05, 2.55, 2.55, 2.55, 2.95, 2.95, 2.95))
 P <- 6447.88

EnvTest <- new.env()
EnvTest$Orig <- 548453.5

FuncTest <- function(pp){
  EnvTest$Orig <- EnvTest$Orig-(P-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 the output:

    [,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

The desired output is:

    [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]  [,10]
[1,] 548453 542942 537422 532116 526799 521470 516304 511126 505934 500941

Update

I am trying to use the updated value of EnvTest$Orig, to calculate the EnvTest$Orig for 10 different values. To do that I am using the variable "R". The output of "x" correctly produces the first three results. But as you see the varible R changes after the first three inputs from 2.05 to 2.55. My function is not properly using the new "R" value and continues to use 2.05 instead of 2.55. This is shown in the code below, which is attempting to calculate the 4th column in the desired output. It is clear that "Proof2" does the properly while "Proof" demonstartes that my function only uses the first instance of the variable "R".

Proof <- 537422-(P-(537422*2.05/1200))
Proof

Which gives an answer of 531892.2 but it should be 532116, as shown below.

 Proof2 <- 537422-(P-(537422*2.55/1200))
 Proof2

Update Round 2

There was nothing wrong with the function, and instead with the variable "R" and this is why I could not get the desired output. Big thanks to RHertel.

Upvotes: 0

Views: 70

Answers (1)

RHertel
RHertel

Reputation: 23788

I still fail to see why you believe that your function is not working correctly. As a check, you can print out the relevant variables at each iteration:

R <- c(2.05, 2.05, 2.05, 2.55, 2.55, 2.55, 2.95, 2.95, 2.95)
P <- 6447.88
EnvTest <- new.env() 
EnvTest$Orig <- 548453.5
FuncTest <- function(pp){
  EnvTest$Orig <- EnvTest$Orig-(P-EnvTest$Orig*R[pp]/1200)
  cat("pp=",pp,"\n")
  cat("R[pp]=",R[pp],"\n")
  cat("EnvTest$Orig",EnvTest$Orig,"\n")
  return(EnvTest$Orig)
}
Test <- sapply(1:9, FuncTest)
#pp= 1 
#R[pp]= 2.05 
#EnvTest$Orig 542942.6 
#pp= 2 
#R[pp]= 2.05 
#EnvTest$Orig 537422.2 
#pp= 3 
#R[pp]= 2.05 
#EnvTest$Orig 531892.4 
#pp= 4 
#R[pp]= 2.55 
#EnvTest$Orig 526574.8 
#pp= 5 
#R[pp]= 2.55 
#EnvTest$Orig 521245.9 
#pp= 6 
#R[pp]= 2.55 
#EnvTest$Orig 515905.7 
#pp= 7 
#R[pp]= 2.95 
#EnvTest$Orig 510726.1 
#pp= 8 
#R[pp]= 2.95 
#EnvTest$Orig 505533.7 
#pp= 9 
#R[pp]= 2.95 
#EnvTest$Orig 500328.6 

Upvotes: 1

Related Questions