0

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 or 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!

0

2 Answers 2

1

I think you want to use if_else().

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

You can easily change what you want the FALSE criteria to return. This will return zero instead of NULL.

df %>% mutate(Area.HRH = if_else(var6 =="High" & var7== "HIGH RISK", Var2, 0)) 
Sign up to request clarification or add additional context in comments.

3 Comments

OP says "and 0 in case it is FALSE", recommend replacing NULL with 0.
Yeah, but it also says this "otherwise return nothing". So I picked one. Thank you for the comment. I'll make an edit.
@Adam. Thank you! what I was looking for!
1

This can be solved in base R with a single ifelse() statement.

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

4 Comments

<- inside ifelse won't work. Use ifelse is a function that returns a result---you need to assign the result. df$var8 <- ifelse(..., df$Var2, 0). (Assigning inside is how this would work with if(){}else{}, but that's not vectorized, so ifelse() is better here.)
It's a lucky bug that it works---only because OP's sample data doesn't include any FALSE values. The ifelse() function first checks the condition, in the case of OP's sample data, the condition is always true, so ifelse ignores the FALSE argument and runs the TRUE argument, df$var8 <- df[, "Var2"]. This result is returned, and because the code includes the assignment, the assignment is made. However, if OP's data included a single item where the condition was not met, both TRUE and FALSE arguments are needed and will be run, in that order...
First df$var8 <- df[, "Var2"] and then df$var8 <- 0. ifelse() will put the results into a vector based on the condition argument and return the correct results (printed on the console because they are not assigned), but since the results of ifelse are not assigned, it will be as if you just ran those two assignment lines, first df$var8 <- df$var2, then df$var8 <- 0 ---and if you look at df all the var8 values will be 0.
See for yourself with a toy example: df = data.frame(a = 1:3); ifelse(df$a < 2, df$b <- 0, df$b <- 1); df

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.