Use lubridate to edit year within dplyr chain

df3 <- df %>%
  mutate(date=ymd(format(df$date, "2014-%m-%d")))
df3

# # A tibble: 15 x 3
#     year       date value
#    <dbl>     <date> <dbl>
#  1  2014 2014-06-08  0.27
#  2  2014 2014-06-15  0.37
#  3  2014 2014-06-22  0.57
#  4  2014 2014-06-29  0.91
#  5  2014 2014-07-06  0.20
#  6  2015 2014-06-14  0.90
#  7  2015 2014-06-21  0.94
#  8  2015 2014-06-28  0.66
#  9  2015 2014-07-05  0.63
# 10  2015 2014-07-12  0.06
# 11  2016 2014-06-12  0.21
# 12  2016 2014-06-19  0.18
# 13  2016 2014-06-26  0.69
# 14  2016 2014-07-03  0.38
# 15  2016 2014-07-10  0.77

all.equal(df2, df3)
# [1] TRUE

Or use do:

df4 <- df %>%
  do({year(.$date)<-2014; .})
df4
# same results as df3

all.equal(df2, df4)
# [1] TRUE

The assignment is just another function call, so you can do:

mutate(df, date = `year<-`(date, 2014))

Gives:

# A tibble: 15 x 3
    year       date value
   <dbl>     <date> <dbl>
 1  2014 2014-06-08  0.27
 2  2014 2014-06-15  0.37
 3  2014 2014-06-22  0.57
 4  2014 2014-06-29  0.91
 5  2014 2014-07-06  0.20
 6  2015 2014-06-14  0.90
 7  2015 2014-06-21  0.94
 8  2015 2014-06-28  0.66
 9  2015 2014-07-05  0.63
10  2015 2014-07-12  0.06
11  2016 2014-06-12  0.21
12  2016 2014-06-19  0.18
13  2016 2014-06-26  0.69
14  2016 2014-07-03  0.38
15  2016 2014-07-10  0.77

Tags:

R

Lubridate

Dplyr