Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can define a new column because it is factor

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading