Dear all,
I am trying to convert a uncommon date format into a standard date. Basically I have a dataset that contains a period with semiannual frequency formatted like: 206 denoting the second half of 2006, 106 denoting the first half and so forth. In order to rearrange it to 2006-06-01 respectively 2006-01-01, i have written a small function:
period2date = function(period)
{
check=list()
check=strsplit(as.character(period),split="")
x=as.numeric(check[[1]][1])
p=ifelse( x >= 2,6,1)
x=2
out=paste(x,"0",check[[1]][2],check[[1]][3],"-",p,"-1",sep="")
out=as.Date(out)
return(out)
}
you may laugh now :) . Anyway, that function works and here comes the problem. I want to apply this function to the time column of data.frame. I tried the following:
as.data.frame(lapply(mydf$period,period2date))
which returned the result closest to what I want: structure.13665..class....Date.. 1 2006-06-01
and so forth.. obviously i´d love to keep the name of my column – or even better just add the newly formatted date to my original df. Plus I tried:
sapply(mydf$period,period2date) # with results equal to the line below
unlist(lapply(mydf$period,period2date))
[1] 13300 13514 13665
All I want to do is change the uncommon 206 etc. format to 2006-06-01 (which works) and add a column to mydf (which does not work)
thx for any suggestions in advance!