HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Calculating calendar year for a student based on DOB

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
studentyearcalculatingforbaseddobcalendar

Problem

I've written this brief function for calculating which calendar year a student would be in Year 1 based on their DOB (date of birth). In 1998 there was a change in the DOB cut points for starting Year 1, hence the need for a function.

year1 = as.Date("1997-07-01")) {ifelse(as.numeric(format(x, "%m")) >=07 , out = as.Date("1997-01-01") & x <= as.Date("1997-06-30")) {out <- 2003}
    return(out)
}


An example of the results:

> year1(as.Date("1996-02-15"))
[1] 2002
> year1(as.Date("1999-02-15"))
[1] 2006


I plan to run it like this:

> test  sapply(test,year1)
[1] 1991 2006 2011


I have a lot of data to run this over, and the use of so many ifs and assigning the result to out just to write it seems pretty redundant, but I couldn't think of a better way to do it.

Solution

I think this is equivalent to your function.

year1 <- function(dob) as.numeric(format(dob, "%Y")) + 6 +
    (as.Date("1997-07-01") <= dob & 7 <= as.numeric(format(dob, "%m")))


You would run it like this.

> test  year1(test)

Code Snippets

year1 <- function(dob) as.numeric(format(dob, "%Y")) + 6 +
    (as.Date("1997-07-01") <= dob & 7 <= as.numeric(format(dob, "%m")))
> test <- c(as.Date("1985-09-15"),as.Date("1999-02-15"),as.Date("2004-08-15"))
> year1(test)

Context

StackExchange Code Review Q#10235, answer score: 3

Revisions (0)

No revisions yet.