mercredi 25 mars 2015

dplyr ifelse not working in mutate

I will start off by saying that I apologize for this example not being reproducible I don't think that I can get the necessary data into stack overflow for this to work.


Anyhow, when I run the following code theta still has negative values.



data <- data %>%
group_by(ob_group) %>%
mutate(
pct_superior = sum(ma_mw, na.rm = T) / length(ma_mw[!is.na(ma_mw)]),
theta = as.numeric(ifelse(benchmark > 0, (pct_superior - benchmark) / benchmark, 0)),
theta = as.numeric(ifelse(theta < 0, 0, theta)))


All of the variables excluding ob_group are numeric.


Thoughts?


Aucun commentaire:

Enregistrer un commentaire