Léo Joubert
Léo Joubert

Reputation: 542

Categorical date variable in R

I have a very simple problem but I can't figure it out... I have two vectors, period and date like :

period <- as.Date("2005-05-01","2009-12-01")
date <- ad.Date("2012-01-05","2003-01-24","2006-04-23")

period is a vector of two breaks with are designing 3 period : from origin to 2005-05-01, from 2005-12-01 to 2009-12-01, from 2009-12-01 to end. I would like to return a vector from date by replacing the dates values by their period, so in this example :

[1] 3 1 2

Can you indicate me how to do this ?

Thank you very much

Upvotes: 3

Views: 484

Answers (2)

Han de Vries
Han de Vries

Reputation: 118

use cut() or findInterval():

period <- as.Date(c("2005-05-01","2009-12-01"))             # note that you need to put the dates in a vector using c()
date <- as.Date(c("2012-01-05","2003-01-24","2006-04-23"))
cut(date, 
    breaks=c(as.Date('1900-01-01'), period, as.Date('3000-01-01')),  # assuming you don't have dates before the year 1900 and after the year 3000
    labels=FALSE)
findInterval(date, 
         c(as.Date('1900-01-01'), period, as.Date('3000-01-01'))))    # faster

Upvotes: 5

JCollerton
JCollerton

Reputation: 3327

You could use a function:

return_period_position <- function(input_date){

  input_date = as.Date(input_date)
  if(input_date < as.Date("2005-05-01") ){ return(1); }
  else if(input_date > as.Date("2005-05-01") && input_date < as.Date("2009-12-01") ){ return(2); }
  else{ return(3); }

}

dates <- c("2012-01-05","2003-01-24","2006-04-23")

unlist(lapply(dates, return_period_position))

Upvotes: 0

Related Questions