Genomics-CRT / Data-Science-For-Life-Science

CRT outreach program to assist life scientists in learning basic data science skills.
28 stars 44 forks source link

R _worksheet week 2_part3[4] #11

Closed LisaLombardi86 closed 4 years ago

LisaLombardi86 commented 4 years ago

Hi guys, I was playing around a bit with the worksheet, and there is something I cannot wrap my head around with regards to the the exercise for granting or denying access to the website for buying alcohol (R _worksheet week 2_part3[4]) I was there last Thursday, and I know what the code is supposed to be, but when I put the numbers in, it just does not make sense.

I noticed that the earlier the DOB, the smaller the calculated age in days (as I can tell from the value of the age variable in the environment window). In fact, when I run the code with my own DOB, I am apparently too young to buy alcohol (very flattering, yet inaccurate). It seems like the age is calculated from some "zero" in the past, so that the further away your DOB is from that point, the higher the number calculated for "age" is. For example, DOB 2002-05-01 corresponds to 11808 days. DOB 1986-10-03 corresponds to 6119 days. DOB 1968-10-03 corresponds to an age of -455 (so I am assuming the "zero" in time is at some point in 1970?). Am I making any sense? If I change the conditional statement from if (age < 6570) to if (age > 6570), it seems to work..so still using the 18yo threshold, but with "opposite direction". I hope you can understand what I mean, I realise I am being very confusing :-)

This is the block of code I am referring to:

age <- as.Date(readline(prompt = "Please Enter Your Age \nUse format YYYY-MM-DD: "))
age <- as.numeric(age)

if (age < 6570) {
  print("You are not old enough to purchase alcohol")
} else {
    print("Entering Website")
}

Could you explain to me what is happening? Thaaaaaaaaanks a lot!!

Lisa

Genomics-CRT commented 4 years ago

Hi Lisa,

Nice catch. I had run that example without any sanity checks! After some reading around the issue, it seems that the as.Date function has an origin of 1970-01-01. This explains the behavior you observed.

Lets rework the code and use today's date as the origin:

age <- as.Date(readline(prompt = "Please Enter Your Age \nUse format YYYY-MM-DD: "))
today <- Sys.Date()
diff <- as.numeric(difftime(today, age, units = "days"))

if(diff < 6570){
  print("You are not old enough to purchase alcohol")
} else{
    print("Entering Website")
}

I have purposely left out comments, can you see the logic there?

Thanks again for the catch, I'll update the document now :)

-- Barry

LisaLombardi86 commented 4 years ago

Thanks a million, Barry!