2

I am working with the following data;

Which is yearly data and a random sample looks like the following;

> sample(years, 10)
 [1] 71 70  8 65 99 15  7  6 63 13

Here 71 corresponds to 1971 and 15 corresponds to 2015 - What I am trying to do is add in the correct 19 and correct 20 before the final digits of the date. So 8 would correspond with 2008 and 99 with 1999. Any push in the right direction would be great.
The data begins in 1960 and ends in 2017.

Data:

years <- c(60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 
    61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 61L, 62L, 
    62L, 62L, 62L, 62L, 62L, 62L, 62L, 62L, 62L, 62L, 62L, 63L, 63L, 
    63L, 63L, 63L, 63L, 63L, 63L, 63L, 63L, 63L, 63L, 64L, 64L, 64L, 
    64L, 64L, 64L, 64L, 64L, 64L, 64L, 64L, 64L, 65L, 65L, 65L, 65L, 
    65L, 65L, 65L, 65L, 65L, 65L, 65L, 65L, 66L, 66L, 66L, 66L, 66L, 
    66L, 66L, 66L, 66L, 66L, 66L, 66L, 67L, 67L, 67L, 67L, 67L, 67L, 
    67L, 67L, 67L, 67L, 67L, 67L, 68L, 68L, 68L, 68L, 68L, 68L, 68L, 
    68L, 68L, 68L, 68L, 68L, 69L, 69L, 69L, 69L, 69L, 69L, 69L, 69L, 
    69L, 69L, 69L, 69L, 70L, 70L, 70L, 70L, 70L, 70L, 70L, 70L, 70L, 
    70L, 70L, 70L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 
    71L, 71L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 
    72L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 
    74L, 74L, 74L, 74L, 74L, 74L, 74L, 74L, 74L, 74L, 74L, 74L, 75L, 
    75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 75L, 76L, 76L, 
    76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 77L, 77L, 77L, 
    77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 77L, 78L, 78L, 78L, 78L, 
    78L, 78L, 78L, 78L, 78L, 78L, 78L, 78L, 79L, 79L, 79L, 79L, 79L, 
    79L, 79L, 79L, 79L, 79L, 79L, 79L, 80L, 80L, 80L, 80L, 80L, 80L, 
    80L, 80L, 80L, 80L, 80L, 80L, 81L, 81L, 81L, 81L, 81L, 81L, 81L, 
    81L, 81L, 81L, 81L, 81L, 82L, 82L, 82L, 82L, 82L, 82L, 82L, 82L, 
    82L, 82L, 82L, 82L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 
    83L, 83L, 83L, 84L, 84L, 84L, 84L, 84L, 84L, 84L, 84L, 84L, 84L, 
    84L, 84L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 
    85L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 
    87L, 87L, 87L, 87L, 87L, 87L, 87L, 87L, 87L, 87L, 87L, 87L, 88L, 
    88L, 88L, 88L, 88L, 88L, 88L, 88L, 88L, 88L, 88L, 88L, 89L, 89L, 
    89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 89L, 90L, 90L, 90L, 
    90L, 90L, 90L, 90L, 90L, 90L, 90L, 90L, 90L, 91L, 91L, 91L, 91L, 
    91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 92L, 92L, 92L, 92L, 92L, 
    92L, 92L, 92L, 92L, 92L, 92L, 92L, 93L, 93L, 93L, 93L, 93L, 93L, 
    93L, 93L, 93L, 93L, 93L, 93L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 
    94L, 94L, 94L, 94L, 94L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 
    95L, 95L, 95L, 95L, 96L, 96L, 96L, 96L, 96L, 96L, 96L, 96L, 96L, 
    96L, 96L, 96L, 97L, 97L, 97L, 97L, 97L, 97L, 97L, 97L, 97L, 97L, 
    97L, 97L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 
    98L, 99L, 99L, 99L, 99L, 99L, 99L, 99L, 99L, 99L, 99L, 99L, 99L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
    12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
    13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
    14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
    15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L)
user113156
  • 6,761
  • 5
  • 35
  • 81
  • AH I forgot to add that in. The data begins in `1960` and ends in `2017` – user113156 Nov 09 '18 at 14:54
  • 1
    [Relevant / Possible duplicate](https://stackoverflow.com/questions/9508747/add-correct-century-to-dates-with-year-provided-as-year-without-century-y). I m not exactly sure so I m not hammering – Sotos Nov 09 '18 at 14:55
  • `Hyears <- ifelse(years>=60, 1900+years, 2000+years)` – jogo Nov 09 '18 at 14:56
  • `yearz <- ifelse(years>18, paste0(19,years), paste0(20,years))` because it's `2018` – M-- Nov 09 '18 at 14:58
  • @Masoud That will give characters (eventually unwanted). – jogo Nov 09 '18 at 14:58
  • @jogo how hard it would be to apply `as.integer()`? (eventually ez, jk) – M-- Nov 09 '18 at 14:59
  • 1
    @Masoud Not much, but if you forget that you will have a problem with e.g. `diff(yearz)` – jogo Nov 09 '18 at 15:03

3 Answers3

6

Try this:

years + 1900 + 100 * (years <= 18)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
1

This can also be used.

nineteen=year[year>=60]+1900
twenty=year[year<60]+2000
year=c(nineteen,twenty)
Alvi
  • 123
  • 1
  • 3
  • 14
0

Maybe this:

year_correct <-lapply(years, function(e) ifelse(e>50, paste0("19",e), paste0("20",e)))

You can change the threshold 50

Jrakru56
  • 1,211
  • 9
  • 16
  • Why `lapply`? How about just `year_correct = ifelse(years > 50, paste0("19", years), paste0("20", years))`? No advantage to having a `list` returned. Or by breaking the vectorization of `paste` into individual calls. – Gregor Thomas Nov 09 '18 at 15:03
  • 1
    And using `+ 1900` and `+ 2000` would probably be better so you get a `numeric` not a `character` out. – Gregor Thomas Nov 09 '18 at 15:05
  • @Gregor you are absolutely right! To be honest, I need more coffee to prevent the *monkey sees `list`, monkey uses `lapply`* syndrome. – Jrakru56 Nov 09 '18 at 15:14