mardi 29 septembre 2020

Ifelse for matrix in R

I have the following code below:

cenarios = 100 #

tamanho = 10000
prob_sin = 0.02
sev_med = 10000

replicacoes_ind = matrix(NA, tamanho, cenarios)

replicacoes_sev = matrix(NA, tamanho, cenarios)

SAg = array(NA, cenarios)

quant_sin = array(NA, cenarios)

u = matrix(data=runif(tamanho*cenarios, 0, 1), nrow=tamanho, ncol=cenarios)
ifelse(u <= prob_sin, {replicacoes_ind = 1; replicacoes_sev = rexp(1,rate = 1/sev_med)}, {replicacoes_ind = 0; replicacoes_sev = 0})

However in my last line (ifelse) what I intended to do is: if each element of the matrix u is <= than prob_sin, then the respective element of the replicacoes_ind matrix is ​​equal to 1 and the respective element of the replicacoes_sev matrix follows a number random. Otherwise, 0 in the respective elements. However, it assigns 1 or 0 to the matrices and transforms them into numbers.

Does anyone know how I can make this condition for each element of the matrices involved?

Thanks in advanced!

Aucun commentaire:

Enregistrer un commentaire