Here is an example of converting the 'date' your dataframe to 'time'
(POSIXct) and then taking the first 'time' and creating the 5 previous days
from that date:
id date case
1 1 27apr19931
2 2 13feb20051
3 3 29may20021
4 4 22sep20051
5 5 18dec19911
6 6 07jun
The sample data you provided has the variable 'date'. Since the type of
this variable is 'factor', you may try to convert type of variable from
'factor' to 'date' as shown in the below:
> x$rval <- as.Date(x$date, format="%d%b%Y")-5
> x
id date case rval
1 1 27apr19931 1993
Dear R Community,
I wish to create 5 preceding dates from the date variable by ID. How
could I create such dates? The code should consider leap year.
Thanks
Sample data follows:
structure(list(id = 1:12, date = structure(c(9L, 6L, 11L, 8L,
7L, 5L, 4L, 3L, 12L, 1L, 10L, 2L), .Label = c("01feb20
3 matches
Mail list logo