Here is an implementation that works:
to.week <- function(x) as.integer(format(x, "%W"))
The reason strtoi failed - by default, it tries to interpret the numbers as if they were octal when they precede "0" . Since "%W" returns "08" and 8 does not exist in octal, you get NA. From ?strtoi :
Convert strings to integers according to the given base using the strtol C function or select the appropriate base according to the C rules.
...
For decimal strings, as.integer is equally useful.
Alternatively, you can use:
week(as.Date("2015-02-25"))
Although you may have to compensate for the result by 1 to meet your expectations.
source share