首页 > 解决方案 > 如果多个条件为真,则从行返回值

问题描述

我有以下虚拟 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 具有示例中未提供的其他因素(例如 Low、Medium、SAFE..)。

我试过这个,但它返回 TRUE 或 FALSE 值,而我只想要 VAR 2 中的值以防它为 TRUE 和 0 以防它为 FALSE。

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

谢谢!

标签: rif-statementdplyr

解决方案


这可以通过R一条ifelse()语句在 base 中解决。

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

推荐阅读