3

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

KhalidN
  • 385
  • 1
  • 7
  • 13

2 Answers2

3
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")
Pierre L
  • 28,203
  • 6
  • 47
  • 69
  • This is a nice syntax but converting to `as.Date` could be expensive for a relatively big data set. I would add `Path2$NewWeek <- strftime(Path2$LogTime + 60*60*24*7, format = "%Y-%W", tz = "CET")` too as an alternative. – David Arenburg Aug 18 '15 at 13:33
  • At first, I didn't assume a date format, but if OP started the Week column, they had to have converted already. – Pierre L Aug 18 '15 at 13:36
  • I meant that your `as.Date(Path$LogTime)` line could be avoided by `Path2$LogTime + 60*60*24*7` which will be more efficient IMO. My only concern here is maybe time zones, don't know. – David Arenburg Aug 18 '15 at 13:37
  • For clarity, "date" in the sense that it is recognized as a date, not the one specific `"Date"` class. Any would work, including `POSIXct`. – Pierre L Aug 18 '15 at 13:55
  • I get "Error in as.POSIXlt.numeric(x, tz = tz) : 'origin' must be supplied" – PM0087 Jan 12 '21 at 18:23
0

As is often the case with R, "There is a package for that". I recommend lubridate. This chapter covers all the usual tasks. This turns your task into one line without losing information:

library(lubridate)   
data$New.Week <- data$LogTime + weeks(1)

By stripping the day/time out of LogTime, you are losing lots of information. And by turning LogTime into type character, you are asking for trouble when you want to perform math operations.

However, I bet you are trying to get YYYY-WW because you are trying to perform some summaries or something. If this is the case, you don't even need to calculate YYYY-WW. Just use group_by from dplyr (which is included in tidyverse).

library(tidyverse)
data %>%
  group_by(year(LogTime),week(LogTime)) %>%
  summarise(count_by_week = n())
Jeff Parker
  • 1,809
  • 1
  • 18
  • 28