How to combine two vectors and get all results from a given account with them

6

I'm trying to do a calculation and I need your help. First I make a normal distribution sequence with 420 numbers, then I make a sequence of numbers that I call mci with 42 numbers, then the next calculation has a if condition.

In this part I need that in my condition always compare the first vector list of mci with the first number of the values that I generated from my normal distribution, then the whole process is repeated with the second number until completing the 420.

For example: For rp = (28, 27, 26 ...), bwg = (count with mci [i]) > rp

I want this rp to always be 28 until the first sequence of 42 numbers ends, and then compare to 27 of the second sequence of my vector.

Note that the condition always always compares the same value of rp , and I think that R is not doing this, comparing for example the value 26,912 with 0,435 where it should compare with the 0,429

max <- 30
K <- 0.0118
Xm <- 21
SD <- 0.851636356306513 
mean.b <- 28

rp <- rnorm(420, mean = mean.b, sd = SD)
BW <- 0.0223*rp^0.8944 
mci <- seq(200, 270, 1.7)
bwg <- ifelse((max*(1-exp(-K*(MCi-(BW*Xm))))) > rp, yes = rp, no = (max*(1-exp(-K*(MCi-(BW*Xm))))))
    
asked by anonymous 20.02.2016 / 03:20

1 answer

0

Well, if I understood your problem well, you want to calculate the expression bwg <- (max*(1-exp(-K*(MCi-(BW*Xm))))) for all combinations of mci and rp , and then keep, for each calculated value, the smallest value between rp and the value of bwg obtained.

If this is the case, you need more than a simple ifelse because you have two vectors with length > 1 to match. In this case, I decided to make a sapply in the rp vector, and then get the bwg values for all mci for each rp .

You can also transform the calculation of bwg into a function, so that you do not repeat the formula. Finally, I used the pmin function instead of a ifelse , because it looks more elegant.

Follow the code with some comments:

maxVal <- 30 # Mudei o nome desse valor, já existe a função max, melhor não misturar
K <- 0.0118
Xm <- 21
SD <- 0.851636356306513 
mean.b <- 28

rp <- rnorm(420, mean = mean.b, sd = SD)
mci <- seq(200, 270, 1.7)

bwg.calc <- function(maxVal, K, MCi, Xm, rp) {
  BW <- 0.0223*rp^0.8944 # Esse valor depende apenas de rp, pode ser obtido aqui
  maxVal*(1-exp(-K*(MCi-(BW*Xm))))
}       

resultado <- sapply(rp, function(i) { # Para cada rp, calculamos contra todos mci
  bwg <- bwg.calc(maxVal = maxVal, K = K, Xm = Xm, MCi = mci, rp = i)
  pmin(i, bwg) # Poderia ser ifelse(bwg > i, i, bwg)
})

I'm not going to put the result, because it's an array with 42 rows (rp size and 420 columns (mci size):

> str(resultado)
 num [1:42, 1:420] 26.8 26.9 27 27 27.1 ...
    
20.02.2016 / 16:21