Skip to content
Prev 383313 / 398502 Next

parsing DOB data

Hi Peter,
One way is to process the strings before converting them to dates:

x2<-c("45-12-03","01-06-24","04-9-15","1901-03-04")
add_century<-function(x,changeover=68,previous=19,current=20) {
 centuries<-sapply(sapply(x,strsplit,"-"),"[",1)
 shortyears<-which(!(nchar(centuries)>2))
 century<-rep("",length(x))
 century[shortyears]<-ifelse(centuries[shortyears]>changeover,previous,current)
 newx<-paste0(century,x)
 return(newx)
}
add_century(x2,1)

Jim

On Fri, Apr 17, 2020 at 12:34 AM Peter Nelson via R-help
<r-help at r-project.org> wrote: