-3

It must be a very easy task, but I can't find the right line of code for this:

Data frame (df) has several columns (Date is the first one, containing string object), and around 200 rows.

  Date V1
1 01/01/2011 5
2 02/01/2011 4
3 03/01/2011 2
...
200 05/09/2011

needs to become this (current year):

  Date V1
1 01/01/2013 5
2 02/01/2013 4
3 03/01/2013 2
...
200 05/09/2013

Thanks!

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
user10745
  • 637
  • 1
  • 5
  • 8
  • 1
    You're gonna have to give us a bit more detail than that: is `Date` an actual date object or is it just a string? And do all the dates end in 2011 and is the replacement 2013 in every case? – RoyalTS May 03 '13 at 18:36
  • 1
    Please see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example on making an example that other users can replicate on their machines so they can help you. If your object is `x`, including `dput(head(x))` in your question would be helpful to determine what kind of object you have. – Blue Magister May 03 '13 at 18:50
  • Just added some edits to my question - please have a look. Thank you – user10745 May 03 '13 at 19:52
  • Hi - this is literally my second post - and I'm a beginner, so might not operate with the right terminology yet. Hope my question is clear now. Thanks. – user10745 May 03 '13 at 20:25

1 Answers1

1
df$Date <- sub('11$','13',df$Date)

should work.

But beware: naming a variable Date is a bad idea because R already has an internal data type with that name.

RoyalTS
  • 9,545
  • 12
  • 60
  • 101
  • No, didn't work either. Plus reported an error: Error in sub("12$", "13", Date) : object 'Date' not found – user10745 May 03 '13 at 20:17
  • 1
    @user10745 RoyalTS was assuming you were familiar with the basics of R and would know how to use a column of a data frame. If what you say in your question is right (i.e. your data.frame's name is `df` and the first column is `Date`, the `Date` column is a character string rather than a `factor` or `Date` object), then `df$Date <- sub('11$', '13', df$Date)` will work. If it doesn't, then you've told us something incorrect. See BlueMagister's comment and link to fix it. – Gregor Thomas May 03 '13 at 20:34
  • @user10745 ...and, when you get an answer that works, "accept" it by clicking the checkmark outline to it's left. It thanks the person who answered and let's everyone know you're not still looking for an answer. – Gregor Thomas May 03 '13 at 21:48
  • Hi Mike - shujaa's code worked because it WAS actually a string, not a date. I made a mistake in my question - now I know what the difference is. Correcting my post again. Thanks for observation – user10745 May 03 '13 at 23:44