vendredi 1 septembre 2017

R: If Else Statement with Four Criteria returning NA

I have this dataset:

Pars <-structure(list(Pars.PeopleSoftID = c(11, 18, 18, 22, 39, 181), 
    Pars..Central.1. = c(0, 1, 1, 1, 1, 1), Pars..Central.2. = c(0, 
    1, 1, 1, 1, 1), Pars.External = c(3, 2, 2, 1, 2, 5), Pars.Minimum = c(15, 
    2, 1.71, 1, 1, 4.39), Pars.Maximum = c(15, 2.76, 2, 1, 1, 
    5.48)), .Names = c("Pars.PeopleSoftID", "Pars..Central.1.", 
"Pars..Central.2.", "Pars.External", "Pars.Minimum", "Pars.Maximum"
), row.names = c(NA, 6L), class = "data.frame")

I'm trying to add a column that calculates a number based on what is in the columns Central 1, Central 2, and External and multiplies that number by the value in Pars$Minimum.

The logic is:

If Central 1 = 0, Central 2 = 0, External = 0 THEN <-- 0 * Pars$Minimum

If Central 1 = 1, Central 2 = 0, External = 0 THEN <-- 1 * Pars$Minimum

If Central 1 = 1, Central 2 = 1, External = 0 THEN <-- 0.7 * Pars$Minimum

If Central 1 = 1, Central 2 = 1, External > 0 THEN <-- 0.7 * Pars$Minimum

If Central 1 = 1, Central 2 = 0, External > 0 THEN <-- 0.8 * Pars$Minimum

I just tried this, but it seems to be just returning values of NA for everything:

 Pars$Cen1 <- with(Pars, if(`Central 1` == 0 , 0, ifelse(`Central 1` == 1 & `Central 2` == 0 & External == 0, 1, ifelse(`Central 1` == 1 & `Central 2` == 1 & External == 0, .7, ifelse(`Central 1` == 1 & `Central 2` == 1 & External > 0, .7, ifelse(`Central 1` == 1 & `Central 2` == 0 & External > 0, .8)

Aucun commentaire:

Enregistrer un commentaire