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

Using if then statements R to changing existing values of variable

I have a data frame with a variable AGE_YR. I want to convert any ages less than 1 to 1.

I am unsure how to write this using an if then/else statement in R and seem to be stuck on using my SAS knowledge which is obviously not helping.

Logically I want to write something like this If df$AGE_YR < 1 THEN df$AGE_YR == 1 but the syntax is obviously wrong.

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

>Solution :

You were not far off

df$AGE_YR <- ifelse(df$AGE_YR < 1, 1, df$AGE_YR)

Might yield better run time

df$AGE_YR <- pmax(df$AGE_YR, 1)
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