-1

In my data file the week variable now is formatted like this

**week** 
May 05 2014 - May 11 2014 
May 12 2014 - May 18 2014 
May 19 2014 - May 25 2014

I want to convert this now to, if possible, two variables week and year signaling the week number and its year, like this

**week**   **year**
19         2014
20         2014 
21         2014

Or maybe one variable week/year giving a combination. Just something that is more workable than this. Would this be possible in any way?

User23
  • 153
  • 2
  • 13

1 Answers1

0

We can extract the substring with sub, convert to Date class, use format to convert to week and year and create a data.frame.

dt <- as.Date(sub("\\s+-.*", "", df1$week), "%b %d %Y")
data.frame(week = as.numeric(format(dt, "%W"))+1, year = format(dt, "%Y"))
#  week year
#1   19 2014
#2   20 2014
#3   21 2014

Or use week and year functions

library(data.table)
dt2 <- as.Date(sub(".*-\\s+", "", df1$week), "%b %d %Y")
data.frame(week = week(dt2), year = year(dt2))

data

df1 <- structure(list(week = c("May 05 2014 - May 11 2014", "May 12 2014 - May 18 2014", 
"May 19 2014 - May 25 2014")), .Names = "week", class = "data.frame", row.names = c(NA, -3L))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks for the suggestion. This what I get now though: `[1] week year <0 rows> (or 0-length row.names)` – User23 Aug 16 '16 at 12:06
  • @user23 Your description is `I want to convert this now to, if possible, two variables week and year signaling the week number and its year, like this` I am not sure I understand your concern. – akrun Aug 16 '16 at 12:08
  • @User23 You can either load `lubridate` or `data.table`. – akrun Aug 16 '16 at 12:55