Home > front end >  Comparing Age variables within a range
Comparing Age variables within a range

Time:07-06

I have two age variables: "age" is from the survey firm's data, "agesurvey" is an age question I asked in the survey. I want to compare them as a data check. However, often the age is /- one year. How can I code this in r?

age <- c(18, 50, 41, 79)
agesurvey <- c(19, 50, 40, 79)

My draft

check <- df %>% mutate ("Agree" = ifelse(age == agesurvey, "Correct", "Incorrect"))
table(check$Agree)

CodePudding user response:

You could use abs(a - b) <= threshold.

ifelse(abs(age - agesurvey) <= 1, "Correct", "Incorrect")

# [1] "Correct" "Correct" "Correct" "Correct"
  • Related