如果多个条件为真,则从行返回值

时间:2019-10-31 16:07:20

标签: r if-statement mutate

我有以下虚拟Df:

structure(list(lat = c(15.04166667, 15.125, 15.29166667, 15.375, 
15.04166667, 15.125, 15.20833333, 15.29166667, 15.375, 15.45833333, 
15.54166667, 14.95833333, 15.04166667, 15.125, 15.20833333, 15.29166667, 
15.375, 15.45833333, 15.54166667, 15.625, 15.70833333, 15.79166667, 
15.875, 16.54166667, 13.875, 14.875, 14.95833333), lon = c(48.95833333, 
48.95833333, 48.95833333, 48.95833333, 48.875, 48.875, 48.875, 
48.875, 48.875, 48.875, 48.875, 48.79166667, 48.79166667, 48.79166667, 
48.79166667, 48.79166667, 48.79166667, 48.79166667, 48.79166667, 
48.79166667, 48.79166667, 48.79166667, 48.79166667, 48.79166667, 
48.70833333, 48.70833333, 48.70833333), Var1 = c(40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L), Var2 = c(29.76510459, 
6.480850609, 223.0983795, 203.8934788, 11.27195619, 65.76071468, 
194.8171225, 262.4171485, 171.163622, 240.1846431, 239.8467942, 
53.94738807, 49.07189175, 118.194278, 218.744134, 313.4466307, 
185.409121, 252.8829675, 219.123076, 211.2351477, 279.0554084, 
260.621935, 169.9482421, 337.1199379, 9.932910029, 96.11876075, 
69.54847552), Var3 = c(6.24087876, 1.358846252, 46.77725586, 
42.75054481, 2.363402045, 13.78811339, 40.84749728, 55.02126264, 
35.88804325, 50.35974897, 50.28891223, 12.29369073, 10.28895202, 
24.78191063, 45.86429711, 65.72066044, 38.87491352, 53.02222021, 
45.94375161, 44.28987901, 58.50982373, 54.64485812, 35.63321409, 
70.68427011, 1.731396537, 20.15331521, 14.58229774), Var4 = c(173.4664468, 
173.4706729, 173.4790964, 173.4833057, 173.4077614, 173.4117034, 
173.4156335, 173.4195758, 173.4235096, 173.4266725, 164.1875386, 
239.5356333, 173.3490717, 173.3527418, 173.3563883, 173.3600476, 
173.3637073, 173.3667678, 164.1276972, 164.1295668, 164.1308363, 
164.1321065, 164.1333879, 167.3271206, 150.2922712, 224.8818893, 
224.8852899), Var5 = c(19.62146524, 19.62146505, 19.62146589, 
19.62146616, 19.62146629, 19.62146473, 19.62146495, 19.62146552, 
19.62146614, 19.62146513, 16.76539618, 38.78913858, 19.62146615, 
19.62146541, 19.62146505, 19.62146575, 19.62146599, 19.62146573, 
16.76539594, 16.76539677, 16.7653963, 16.76539632, 16.76539658, 
17.25115902, 11.78819987, 34.82506079, 34.82506128), var6 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "High", class = "factor"), 
    var7 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = "HIGH RISK", class = "factor")), class = "data.frame", row.names = c(NA, 
-27L))

我想做的是创建一个名为var8的新变量,该变量仅在Var6为“高”且Var7为“高风险”时才返回Var2。请注意,var6和var7还有其他因素(例如,低,中,安全)。示例中未提供。

我尝试了此方法,但是它返回的是TRUE或FALSE值,而我只希望VAr 2中的值为TRUE(真)和0(FALSE)。

  df<- df %>% 
        mutate( Area.HRH=Var6 =="High"  & 
        (Var7== "HIGH RISK"))

谢谢!

2 个答案:

答案 0 :(得分:1)

我认为您想使用if_else()

library(dplyr)

df %>% 
  mutate(Area.HRH = if_else(var6 =="High" & var7== "HIGH RISK", Var2, NULL))

您可以轻松更改希望FALSE返回的条件。这将返回零而不是NULL

df %>% 
  mutate(Area.HRH = if_else(var6 =="High" & var7== "HIGH RISK", Var2, 0))

答案 1 :(得分:1)

这可以在基础R中使用单个ifelse()语句来解决。

df$var8 <- ifelse(df[, "var6"] == "High" & df[, "var7"] == "HIGH RISK",
                  df[, "Var2"],
                  0)