You can simply use the format function as follows:
format(Date, '%Y%j')
which gives:
[1] "2016161" "2016162" "2016163"
If you want to format it in other ways, see ?strptime for all possible parameters.
Alternatively, you can use the year and yday from the data.table or lubridate and paste them together with paste0 :
library(data.table)
which will give you the same result.
The values ββreturned by both parameters have a class sign. Wrap the above solutions in as.numeric() to get real numbers.
Used data:
> Date <- Sys.Date() + 1:3 > Date [1] "2016-06-09" "2016-06-10" "2016-06-11" > class(Date) [1] "Date"
source share