Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Year-Week add a week in r

I have a dataframe with a column containing Date and time, and I have created a new column containing Year-Week with this formula,

Path     LogTime
HJH      2015-06-19 01:57:11
GTF      2015-07-31 08:23:34
R        2015-07-21 11:52:31

Path2$Week<-strftime(Path2$LogTime,format="%Y-%W",tz="CET")
Path     LogTime                Week
HJH      2015-06-19 01:57:11    2015-24
GTF      2015-07-31 08:23:34    2015-30
R        2015-07-21 11:52:31    2015-29

I now want to add another week to it, so instead of it saying

Path     LogTime                Week       NEW Week
HJH      2015-06-19 01:57:11    2015-24    2015-25
GTF      2015-07-31 08:23:34    2015-30    2015-31
R        2015-07-21 11:52:31    2015-29    2015-30

How can I do this in R? I have trouble finding my way in the maze of the dates in R

like image 430
KhalidN Avatar asked Nov 26 '25 20:11

KhalidN


1 Answers

Path$New.Week <- strftime(as.Date(Path$LogTime)+7, "%Y-%W", tz="CET")
   Path            LogTime    Week New.Week
1  HJH 2015-06-19 01:57:11 2015-24  2015-25
2  GTF 2015-07-31 08:23:34 2015-30  2015-31
3    R 2015-07-21 11:52:31 2015-29  2015-30

As mentioned in the comments by @DavidArenburg, if the LogTime column is in any proper date format like POSIXct as it appears to be in your example, and not a string or factor, you can save an operation with:

Path$NewWeek <- strftime(Path$LogTime + 60*60*24*7, format = "%Y-%W", tz = "CET")
like image 50
Pierre L Avatar answered Nov 29 '25 10:11

Pierre L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!