Reputation: 35
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)
Upvotes: 1
Views: 718
Reputation: 1758
Another possibility is to make a map of values and lookup the conditions pasted together there:
valuemap = c("000" = 0, "001"= 0, "100"= 1, "110"= 0.7, "111" = 0.7, "101" = 0.8)
Pars$Cen1 <- Pars$Pars.Minimum *
valuemap[paste0(Pars$Pars..Central.1.,Pars$Pars..Central.2.,
1 * (Pars$Pars.External > 0))]
Upvotes: 3
Reputation: 820
Your final ifelse statement has only two arguments, so if you ever get to trying to assign the value of that 'no' statement, you'll get an error.
This seemed to work for me:
Pars$Cen1 <- ifelse(Pars$Pars..Central.1. == 0, 0,
ifelse(Pars$Pars..Central.1. == 1 & Pars$Pars..Central.2. == 0 & Pars$Pars.External == 0, 1,
ifelse(Pars$Pars..Central.1. == 1 & Pars$Pars..Central.2. == 1 & Pars$Pars.External == 0, .7,
ifelse(Pars$Pars..Central.1. == 1 & Pars$Pars..Central.2. == 1 & Pars$Pars.External > 0, .7,
ifelse(Pars$Pars..Central.1. == 1 & Pars$Pars..Central.2. == 0 & Pars$Pars.External > 0, .8, "Missing")))))
Upvotes: 1
Reputation: 635
I renamed some of your columns as well, wasnt sure why each column name needed to be called "Pars." if the table was already called Pars, but this works here to do what you want, should give you a format to use.
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("PeopleSoftID", "Central.1",
"Central.2", "External", "Minimum", "Maximum"
), row.names = c(NA, 6L), class = "data.frame")
Pars$Cen1 <- with(Pars,ifelse(Central.1 == 0 & Central.2 == 0 & External == 0,0*Minimum,
ifelse(Central.1 == 1 & Central.2 == 0 & External == 0,1*Minimum,
ifelse(Central.1 == 1 & Central.2 == 1 & External >= 0,0.7*Minimum,
ifelse(Central.1 == 1 & Central.2 == 0 & External > 0,.8*Minimum,0)))))
Upvotes: 3
Reputation: 28339
library(dplyr)
mutate(Pars, Cen1 = case_when(
`Central 1` == 0 & `Central 2` == 0 & External == 0 ~ 0 * Minimum,
`Central 1` == 1 & `Central 2` == 0 & External == 0 ~ 1 * Minimum,
`Central 1` == 1 & `Central 2` == 1 & External == 0 ~ 0.7 * Minimum,
`Central 1` == 1 & `Central 2` == 1 & External > 0 ~ 0.7 * Minimum,
`Central 1` == 1 & `Central 2` == 0 & External > 0 ~ 0.8 * Minimum))
Upvotes: 1