KhalidN
KhalidN

Reputation: 405

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

Upvotes: 3

Views: 4211

Answers (2)

Jeff Parker
Jeff Parker

Reputation: 1969

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())

Upvotes: 0

Pierre L
Pierre L

Reputation: 28441

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")

Upvotes: 3

Related Questions