我有这个数据集:
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")
我尝试添加一个列,根据Central 1,Central 2和External列中的内容计算一个数字,并将该数字乘以Pars $ Minimum中的值。
逻辑是:
如果Central 1 = 0,Central 2 = 0,External = 0 THEN&lt; - 0 * Pars $ Minimum
如果Central 1 = 1,Central 2 = 0,External = 0 THEN&lt; - 1 * Pars $ Minimum
如果Central 1 = 1,Central 2 = 1,External = 0 THEN&lt; - 0.7 * Pars $ Minimum
如果Central 1 = 1,Central 2 = 1,External&gt; 0 THEN&lt; - 0.7 * Pars $ Minimum
如果Central 1 = 1,Central 2 = 0,External&gt; 0 THEN&lt; - 0.8 * Pars $ Minimum
我刚试过这个,但似乎只是为了一切返回NA的值:
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)
答案 0 :(得分:3)
我也重命名了一些列,不知道为什么需要调用每个列名称&#34; Pars。&#34;如果该表已被称为Pars,但这可以在这里做你想要的,应该给你一个格式使用。
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)))))
答案 1 :(得分:3)
另一种可能性是制作一个值映射并查找粘贴在那里的条件:
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))]
答案 2 :(得分:1)
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))
答案 3 :(得分:1)
你的最终ifelse陈述只有两个参数,所以如果你试图分配那个&#39; no&#39;声明,您将收到错误。
这似乎对我有用:
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")))))