jeudi 31 octobre 2019

Return value from row if multiple conditions are true

I have the following dummy 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))

What I want to do is create a new variable called var8 which return Var2 only if Var6 is "High" and Var7 is "HIGH RISK" otherwise return nothing. Note that var6 and var7 have other factor( e.g. Low, Medium, SAFE.. )which are not provided in the example.

I tried this, but it return TRUE o FALSE value, whereas I want the value from VAr 2 only in case it being TRUE and 0 in case it is FALSE.

  df<- df %>% 

mutate( Area.HRH=Var6 =="High" & (Var7== "HIGH RISK"))

Thank you!

Aucun commentaire:

Enregistrer un commentaire