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

Asked

Viewed 582 times

5

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

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

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

I want that rp Always be 28 until you finish the first sequence of 42 numbers, and then compare with 27 of the second sequence of my vector. inserir a descrição da imagem aqui

Note that the condition always compares the same value of rp, and I think the 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))))))
  • 2

    Downvote until you edit the question title.

  • 5

    Welcome to Stack Overflow! First, do not use caps lock in the title of your question, second, clarify with minimal words what you want to do in the title of the question, third, make it clear where the problem is, what you are trying to do and what went wrong.

1 answer

0

Well, if I understand your problem correctly, 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 lowest value between rp and the value of bwg obtained.

If that’s 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 vector rp, and then get the values of bwg for all mci for each rp.

You can also transform the calculation of bwg in a function, not to keep repeating the formula. Finally, I used the function pmin 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 will not put the result because it is an array with 42 rows (size of Rp and 420 columns (size of mci):

> str(resultado)
 num [1:42, 1:420] 26.8 26.9 27 27 27.1 ...
  • Molx, if you can help me, can you give me a contact email? hugs!

  • 1

    @MARCOSSANTOS The idea of the OS is that the solution to your problem also serves to help other people who may have a similar problem, so ideally we keep in touch around here. If it is something that is unclear or incorrect in the answer, you can comment here. If it is not related to this problem, ask a new question. With that too you can be helped by hundreds of people, and not just me.

  • Dear Molx, you’re right we’ll try to settle here. However I think the code is not exactly correct because the values remain strange, I changed the image to see if it gets better, if you can help me I am grateful.

  • @Why strange Marcossantos? Values will not be identical, you use rnorm to generate rp and so the values are random, every time you rotate the result will be a little different. You can test with a standard set of data instead of random data, or else see code step by step to understand if it does what you want.

  • 1

    @MARCOSSANTHS, the calculation may become reproducible by means of set.Seed(). The R generates pseudorandom numbers, so defining the same seed would be possible to reproduce exactly the data generated in the example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.