I have a dataset like this:
| risk earthquake | platarea | magnitude | area |
|---|---|---|---|
| 0.4 | no | 5 | 30 |
| 0.5 | no | 6 | 20 |
| 5.5 | yes | 6 | 20 |
I would like to create a new column
i gave that code
df$newrisk <- 0.5*df$magnitude + 0.6*df$aarea + 3*df$platarea
I got an error message for df$platarea?
BUt the platarea will only increase when it is "yes".
How can I code that???? the code is right if I omit df$platarea, but i would also include df$platarea but don’t know how??
>Solution :
We can create a logical vector
i1 <- df$platarea == "yes"
df$newrisk[i1] <- with(df, 0.5 * magnitude[i1] + 0.6 * area[i] + 3)
If it is only to change the + 3 *, multiply by the logical vector so that FALSE (or 0 will return 0 and TRUE for ‘yes’ will return 3 as -3 *1 = 3)
df$newrisk <- with(df, 0.5 * magnitude + 0.6 * area + 3 *i1)