0
 [1] "18-12-20" "18-12-20" "04-12-20" "27-11-20" "12-11-20" "30-10-20" "30-10-20"
 [8] "30-10-20" "22-10-20" "16-10-20" "09-10-20" "02-10-20" "02-10-20" "25-09-20"
[15] "25-09-20" "18-09-20" "11-09-20" "04-09-20" "28-08-20" "21-08-20" "21-08-20"
[22] "14-08-20" "07-08-20" "31-07-20" "17-07-20" "03-07-20" "26-06-20" "19-06-20"
[29] "12-06-20" "05-06-20" "28-05-20" "22-05-20" "15-05-20" "08-05-20" "08-05-20"
[36] "01-05-20" "17-04-20" "10-04-20" "20-03-20" "13-03-20" "06-03-20" "27-02-20"
[43] "27-02-20" "20-02-20" "13-02-20" "06-02-20" "30-01-20" "30-01-20" "23-01-20"
[50] "23-01-20" "16-01-20" "09-01-20"

How to replace the last part of the string with a new string ? I want to replace the last part "20" with "2015"

3 Answers3

1

gsub will work fine for this

gsub("-20", "-2015", df)
rar
  • 894
  • 1
  • 9
  • 24
0

You could use sub.

*20 means it can accommodate anything before 20 whereas $ in the end ensures that it is the end of vector and nothing is present after that. Hence, this helps in case if there is date with value 20.

sub("*20$", "2015", x)

where x is the vector you want to replace.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

Another method is to paste "15" to the end of each element:

temp <- c("18-12-20", "18-12-20", "04-12-20")
paste(temp, "15", sep="")
[1] "18-12-2015" "18-12-2015" "04-12-2015"

or

paste0(temp, "15")
lmo
  • 37,904
  • 9
  • 56
  • 69