Home > Blockchain >  Using if then statements R to changing existing values of variable
Using if then statements R to changing existing values of variable

Time:02-04

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.

CodePudding user response:

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)
  •  Tags:  
  • Related