Skip to content
Prev 363334 / 398502 Next

need some help with date

I suppose you intended DateCentury(Date = df[,?TERM DATE?]) there
Your code isn't vectorized; a vector argument will cause trouble as soon as you get to the if() statements inside the function. 

A quick (but slow...) way out is to use Vectorize(DateCentury)(...) (or a for loop, or sapply()).  A better way is to learn how to write code in a vectorized fashion. This typically involves replacing if() with indexing ([...]) or maybe ifelse() constructions (sorry, fixing your actual code would take a bit too much time).

A couple of other hints:

- Don't write code that relies on current date. You'll regret it when you need to run it again in 20 years and 2035 is no longer in the future.

- The string manipulation looks like it would be more conveniently done by converting the dates to POSIXlt objects, manipulate the year field directly, and then convert to character using a suitable format. Or, can't you just add/subtract 100 years = 36525 days? That should work as long as you don't need to go back further than March 1, 1900 (when you'll get bitten by the fact that 1900 was not a leap year but 2000 was...). 

-pd