R 中 1900 年代、2000 年代和 2010 年代的日期格式
Date format over 1900's, 2000's and 2010's in R
我有一个日期从 1950 年代到 2017 年的数据集。我正在尝试将格式转换为 %m/%d/%Y
,但在指定 2000 年代到 2010 年代不同的日期时我被绊倒了。
library(tidyverse)
library(ggplot2)
discharge <- discharge %>%
mutate(YYYY.MM.DD = sub("^(\d+/\d+/)(\d+)$", "\119\2", YYYY.MM.DD)) %>%
mutate(YYYY.MM.DD = sub("190","200","201", YYYY.MM.DD)) %>%
mutate(YYYY.MM.DD = strptime(YYYY.MM.DD, format = "%m/%d/%Y"))
这会输出 NA,而删除“201”或“200”只会使值再次循环到 19 世纪。如有任何帮助,我们将不胜感激!
> dput(discharge)
structure(list(YYYY.MM.DD = structure(c(19L, 287L, 354L, 421L,
488L, 555L, 622L, 689L, 756L, 86L, 153L, 220L, 20L, 288L, 355L,
422L, 489L, 556L, 623L, 690L, 757L, 87L, 154L, 221L, 21L, 289L,
356L, 423L, 490L, 557L, 624L, 691L, 758L, 88L, 155L, 222L, 22L,
290L, 357L, 424L, 491L, 558L, 625L, 692L, 759L, 89L, 156L, 223L,
23L, 291L, 358L, 425L, 492L, 559L, 626L, 693L, 760L, 90L, 157L,
224L, 24L, 292L, 359L, 426L, 493L, 560L, 627L, 694L, 761L, 91L,
158L, 225L, 25L, 293L, 360L, 427L, 494L, 561L, 628L, 695L, 762L,
92L, 159L, 226L, 26L, 294L, 361L, 428L, 495L, 562L, 629L, 696L,
763L, 93L, 160L, 227L, 27L, 295L, 362L, 429L, 496L, 563L, 630L,
697L, 764L, 94L, 161L, 228L, 28L, 296L, 363L, 430L, 497L, 564L,
631L, 698L, 765L, 95L, 162L, 229L, 29L, 297L, 364L, 431L, 498L,
565L, 632L, 699L, 766L, 96L, 163L, 230L, 30L, 298L, 365L, 432L,
499L, 566L, 633L, 700L, 767L, 97L, 164L, 231L, 31L, 299L, 366L,
433L, 500L, 567L, 634L, 701L, 768L, 98L, 165L, 232L, 32L, 300L,
367L, 434L, 501L, 568L, 635L, 702L, 769L, 99L, 166L, 233L, 33L,
301L, 368L, 435L, 502L, 569L, 636L, 703L, 770L, 100L, 167L, 234L,
34L, 302L, 369L, 436L, 503L, 570L, 637L, 704L, 771L, 101L, 168L,
235L, 35L, 303L, 370L, 437L, 504L, 571L, 638L, 705L, 772L, 102L,
169L, 236L, 36L, 304L, 371L, 438L, 505L, 572L, 639L, 706L, 773L,
103L, 170L, 237L, 37L, 305L, 372L, 439L, 506L, 573L, 640L, 707L,
774L, 104L, 171L, 238L, 38L, 306L, 373L, 440L, 507L, 574L, 641L,
708L, 775L, 105L, 172L, 239L, 39L, 307L, 374L, 441L, 508L, 575L,
642L, 709L, 776L, 106L, 173L, 240L, 40L, 308L, 375L, 442L, 509L,
576L, 643L, 710L, 777L, 107L, 174L, 241L, 41L, 309L, 376L, 443L,
510L, 577L, 644L, 711L, 778L, 108L, 175L, 242L, 42L, 310L, 377L,
444L, 511L, 578L, 645L, 712L, 779L, 109L, 176L, 243L, 43L, 311L,
378L, 445L, 512L, 579L, 646L, 713L, 780L, 110L, 177L, 244L, 44L,
312L, 379L, 446L, 513L, 580L, 647L, 714L, 781L, 111L, 178L, 245L,
45L, 313L, 380L, 447L, 514L, 581L, 648L, 715L, 782L, 112L, 179L,
246L, 46L, 314L, 381L, 448L, 515L, 582L, 649L, 716L, 783L, 113L,
180L, 247L, 47L, 315L, 382L, 449L, 516L, 583L, 650L, 717L, 784L,
114L, 181L, 248L, 48L, 316L, 383L, 450L, 517L, 584L, 651L, 718L,
785L, 115L, 182L, 249L, 49L, 317L, 384L, 451L, 518L, 585L, 652L,
719L, 786L, 116L, 183L, 250L, 50L, 318L, 385L, 452L, 519L, 586L,
653L, 720L, 787L, 117L, 184L, 251L, 51L, 319L, 386L, 453L, 520L,
587L, 654L, 721L, 788L, 118L, 185L, 252L, 52L, 320L, 387L, 454L,
521L, 588L, 655L, 722L, 789L, 119L, 186L, 253L, 53L, 321L, 388L,
455L, 522L, 589L, 656L, 723L, 790L, 120L, 187L, 254L, 54L, 322L,
389L, 456L, 523L, 590L, 657L, 724L, 791L, 121L, 188L, 255L, 55L,
323L, 390L, 457L, 524L, 591L, 658L, 725L, 792L, 122L, 189L, 256L,
56L, 324L, 391L, 458L, 525L, 592L, 659L, 726L, 793L, 123L, 190L,
257L, 57L, 325L, 392L, 459L, 526L, 593L, 660L, 727L, 794L, 124L,
191L, 258L, 58L, 326L, 393L, 460L, 527L, 594L, 661L, 728L, 795L,
125L, 192L, 259L, 59L, 327L, 394L, 461L, 528L, 595L, 662L, 729L,
796L, 126L, 193L, 260L, 60L, 328L, 395L, 462L, 529L, 596L, 663L,
730L, 797L, 127L, 194L, 261L, 61L, 329L, 396L, 463L, 530L, 597L,
664L, 731L, 798L, 128L, 195L, 262L, 62L, 330L, 397L, 464L, 531L,
598L, 665L, 732L, 799L, 129L, 196L, 263L, 63L, 331L, 398L, 465L,
532L, 599L, 666L, 733L, 800L, 130L, 197L, 264L, 64L, 332L, 399L,
466L, 533L, 600L, 667L, 734L, 801L, 131L, 198L, 265L, 65L, 333L,
400L, 467L, 534L, 601L, 668L, 735L, 802L, 132L, 199L, 266L, 66L,
334L, 401L, 468L, 535L, 602L, 669L, 736L, 803L, 133L, 200L, 267L,
67L, 335L, 402L, 469L, 536L, 603L, 670L, 737L, 804L, 134L, 201L,
268L, 1L, 269L, 336L, 403L, 470L, 537L, 604L, 671L, 738L, 68L,
135L, 202L, 2L, 270L, 337L, 404L, 471L, 538L, 605L, 672L, 739L,
69L, 136L, 203L, 3L, 271L, 338L, 405L, 472L, 539L, 606L, 673L,
740L, 70L, 137L, 204L, 4L, 272L, 339L, 406L, 473L, 540L, 607L,
674L, 741L, 71L, 138L, 205L, 5L, 273L, 340L, 407L, 474L, 541L,
608L, 675L, 742L, 72L, 139L, 206L, 6L, 274L, 341L, 408L, 475L,
542L, 609L, 676L, 743L, 73L, 140L, 207L, 7L, 275L, 342L, 409L,
476L, 543L, 610L, 677L, 744L, 74L, 141L, 208L, 8L, 276L, 343L,
410L, 477L, 544L, 611L, 678L, 745L, 75L, 142L, 209L, 9L, 277L,
344L, 411L, 478L, 545L, 612L, 679L, 746L, 76L, 143L, 210L, 10L,
278L, 345L, 412L, 479L, 546L, 613L, 680L, 747L, 77L, 144L, 211L,
11L, 279L, 346L, 413L, 480L, 547L, 614L, 681L, 748L, 78L, 145L,
212L, 12L, 280L, 347L, 414L, 481L, 548L, 615L, 682L, 749L, 79L,
146L, 213L, 13L, 281L, 348L, 415L, 482L, 549L, 616L, 683L, 750L,
80L, 147L, 214L, 14L, 282L, 349L, 416L, 483L, 550L, 617L, 684L,
751L, 81L, 148L, 215L, 15L, 283L, 350L, 417L, 484L, 551L, 618L,
685L, 752L, 82L, 149L, 216L, 16L, 284L, 351L, 418L, 485L, 552L,
619L, 686L, 753L, 83L, 150L, 217L, 17L, 285L, 352L, 419L, 486L,
553L, 620L, 687L, 754L, 84L, 151L, 218L, 18L, 286L, 353L, 420L,
487L, 554L, 621L, 688L, 755L, 85L, 152L, 219L), .Label = c("1/1/00",
"1/1/01", "1/1/02", "1/1/03", "1/1/04", "1/1/05", "1/1/06", "1/1/07",
"1/1/08", "1/1/09", "1/1/10", "1/1/11", "1/1/12", "1/1/13", "1/1/14",
"1/1/15", "1/1/16", "1/1/17", "1/1/51", "1/1/52", "1/1/53", "1/1/54",
"1/1/55", "1/1/56", "1/1/57", "1/1/58", "1/1/59", "1/1/60", "1/1/61",
"1/1/62", "1/1/63", "1/1/64", "1/1/65", "1/1/66", "1/1/67", "1/1/68",
"1/1/69", "1/1/70", "1/1/71", "1/1/72", "1/1/73", "1/1/74", "1/1/75",
"1/1/76", "1/1/77", "1/1/78", "1/1/79", "1/1/80", "1/1/81", "1/1/82",
"1/1/83", "1/1/84", "1/1/85", "1/1/86", "1/1/87", "1/1/88", "1/1/89",
"1/1/90", "1/1/91", "1/1/92", "1/1/93", "1/1/94", "1/1/95", "1/1/96",
"1/1/97", "1/1/98", "1/1/99", "10/1/00", "10/1/01", "10/1/02",
"10/1/03", "10/1/04", "10/1/05", "10/1/06", "10/1/07", "10/1/08",
"10/1/09", "10/1/10", "10/1/11", "10/1/12", "10/1/13", "10/1/14",
"10/1/15", "10/1/16", "10/1/17", "10/1/51", "10/1/52", "10/1/53",
"10/1/54", "10/1/55", "10/1/56", "10/1/57", "10/1/58", "10/1/59",
"10/1/60", "10/1/61", "10/1/62", "10/1/63", "10/1/64", "10/1/65",
"10/1/66", "10/1/67", "10/1/68", "10/1/69", "10/1/70", "10/1/71",
"10/1/72", "10/1/73", "10/1/74", "10/1/75", "10/1/76", "10/1/77",
"10/1/78", "10/1/79", "10/1/80", "10/1/81", "10/1/82", "10/1/83",
"10/1/84", "10/1/85", "10/1/86", "10/1/87", "10/1/88", "10/1/89",
"10/1/90", "10/1/91", "10/1/92", "10/1/93", "10/1/94", "10/1/95",
"10/1/96", "10/1/97", "10/1/98", "10/1/99", "11/1/00", "11/1/01",
"11/1/02", "11/1/03", "11/1/04", "11/1/05", "11/1/06", "11/1/07",
"11/1/08", "11/1/09", "11/1/10", "11/1/11", "11/1/12", "11/1/13",
"11/1/14", "11/1/15", "11/1/16", "11/1/17", "11/1/51", "11/1/52",
"11/1/53", "11/1/54", "11/1/55", "11/1/56", "11/1/57", "11/1/58",
"11/1/59", "11/1/60", "11/1/61", "11/1/62", "11/1/63", "11/1/64",
"11/1/65", "11/1/66", "11/1/67", "11/1/68", "11/1/69", "11/1/70",
"11/1/71", "11/1/72", "11/1/73", "11/1/74", "11/1/75", "11/1/76",
"11/1/77", "11/1/78", "11/1/79", "11/1/80", "11/1/81", "11/1/82",
"11/1/83", "11/1/84", "11/1/85", "11/1/86", "11/1/87", "11/1/88",
"11/1/89", "11/1/90", "11/1/91", "11/1/92", "11/1/93", "11/1/94",
"11/1/95", "11/1/96", "11/1/97", "11/1/98", "11/1/99", "12/1/00",
"12/1/01", "12/1/02", "12/1/03", "12/1/04", "12/1/05", "12/1/06",
"12/1/07", "12/1/08", "12/1/09", "12/1/10", "12/1/11", "12/1/12",
"12/1/13", "12/1/14", "12/1/15", "12/1/16", "12/1/17", "12/1/51",
"12/1/52", "12/1/53", "12/1/54", "12/1/55", "12/1/56", "12/1/57",
"12/1/58", "12/1/59", "12/1/60", "12/1/61", "12/1/62", "12/1/63",
"12/1/64", "12/1/65", "12/1/66", "12/1/67", "12/1/68", "12/1/69",
"12/1/70", "12/1/71", "12/1/72", "12/1/73", "12/1/74", "12/1/75",
"12/1/76", "12/1/77", "12/1/78", "12/1/79", "12/1/80", "12/1/81",
"12/1/82", "12/1/83", "12/1/84", "12/1/85", "12/1/86", "12/1/87",
"12/1/88", "12/1/89", "12/1/90", "12/1/91", "12/1/92", "12/1/93",
"12/1/94", "12/1/95", "12/1/96", "12/1/97", "12/1/98", "12/1/99",
"2/1/00", "2/1/01", "2/1/02", "2/1/03", "2/1/04", "2/1/05", "2/1/06",
"2/1/07", "2/1/08", "2/1/09", "2/1/10", "2/1/11", "2/1/12", "2/1/13",
"2/1/14", "2/1/15", "2/1/16", "2/1/17", "2/1/51", "2/1/52", "2/1/53",
"2/1/54", "2/1/55", "2/1/56", "2/1/57", "2/1/58", "2/1/59", "2/1/60",
"2/1/61", "2/1/62", "2/1/63", "2/1/64", "2/1/65", "2/1/66", "2/1/67",
"2/1/68", "2/1/69", "2/1/70", "2/1/71", "2/1/72", "2/1/73", "2/1/74",
"2/1/75", "2/1/76", "2/1/77", "2/1/78", "2/1/79", "2/1/80", "2/1/81",
"2/1/82", "2/1/83", "2/1/84", "2/1/85", "2/1/86", "2/1/87", "2/1/88",
"2/1/89", "2/1/90", "2/1/91", "2/1/92", "2/1/93", "2/1/94", "2/1/95",
"2/1/96", "2/1/97", "2/1/98", "2/1/99", "3/1/00", "3/1/01", "3/1/02",
"3/1/03", "3/1/04", "3/1/05", "3/1/06", "3/1/07", "3/1/08", "3/1/09",
"3/1/10", "3/1/11", "3/1/12", "3/1/13", "3/1/14", "3/1/15", "3/1/16",
"3/1/17", "3/1/51", "3/1/52", "3/1/53", "3/1/54", "3/1/55", "3/1/56",
"3/1/57", "3/1/58", "3/1/59", "3/1/60", "3/1/61", "3/1/62", "3/1/63",
"3/1/64", "3/1/65", "3/1/66", "3/1/67", "3/1/68", "3/1/69", "3/1/70",
"3/1/71", "3/1/72", "3/1/73", "3/1/74", "3/1/75", "3/1/76", "3/1/77",
"3/1/78", "3/1/79", "3/1/80", "3/1/81", "3/1/82", "3/1/83", "3/1/84",
"3/1/85", "3/1/86", "3/1/87", "3/1/88", "3/1/89", "3/1/90", "3/1/91",
"3/1/92", "3/1/93", "3/1/94", "3/1/95", "3/1/96", "3/1/97", "3/1/98",
"3/1/99", "4/1/00", "4/1/01", "4/1/02", "4/1/03", "4/1/04", "4/1/05",
"4/1/06", "4/1/07", "4/1/08", "4/1/09", "4/1/10", "4/1/11", "4/1/12",
"4/1/13", "4/1/14", "4/1/15", "4/1/16", "4/1/17", "4/1/51", "4/1/52",
"4/1/53", "4/1/54", "4/1/55", "4/1/56", "4/1/57", "4/1/58", "4/1/59",
"4/1/60", "4/1/61", "4/1/62", "4/1/63", "4/1/64", "4/1/65", "4/1/66",
"4/1/67", "4/1/68", "4/1/69", "4/1/70", "4/1/71", "4/1/72", "4/1/73",
"4/1/74", "4/1/75", "4/1/76", "4/1/77", "4/1/78", "4/1/79", "4/1/80",
"4/1/81", "4/1/82", "4/1/83", "4/1/84", "4/1/85", "4/1/86", "4/1/87",
"4/1/88", "4/1/89", "4/1/90", "4/1/91", "4/1/92", "4/1/93", "4/1/94",
"4/1/95", "4/1/96", "4/1/97", "4/1/98", "4/1/99", "5/1/00", "5/1/01",
"5/1/02", "5/1/03", "5/1/04", "5/1/05", "5/1/06", "5/1/07", "5/1/08",
"5/1/09", "5/1/10", "5/1/11", "5/1/12", "5/1/13", "5/1/14", "5/1/15",
"5/1/16", "5/1/17", "5/1/51", "5/1/52", "5/1/53", "5/1/54", "5/1/55",
"5/1/56", "5/1/57", "5/1/58", "5/1/59", "5/1/60", "5/1/61", "5/1/62",
"5/1/63", "5/1/64", "5/1/65", "5/1/66", "5/1/67", "5/1/68", "5/1/69",
"5/1/70", "5/1/71", "5/1/72", "5/1/73", "5/1/74", "5/1/75", "5/1/76",
"5/1/77", "5/1/78", "5/1/79", "5/1/80", "5/1/81", "5/1/82", "5/1/83",
"5/1/84", "5/1/85", "5/1/86", "5/1/87", "5/1/88", "5/1/89", "5/1/90",
"5/1/91", "5/1/92", "5/1/93", "5/1/94", "5/1/95", "5/1/96", "5/1/97",
"5/1/98", "5/1/99", "6/1/00", "6/1/01", "6/1/02", "6/1/03", "6/1/04",
"6/1/05", "6/1/06", "6/1/07", "6/1/08", "6/1/09", "6/1/10", "6/1/11",
"6/1/12", "6/1/13", "6/1/14", "6/1/15", "6/1/16", "6/1/17", "6/1/51",
"6/1/52", "6/1/53", "6/1/54", "6/1/55", "6/1/56", "6/1/57", "6/1/58",
"6/1/59", "6/1/60", "6/1/61", "6/1/62", "6/1/63", "6/1/64", "6/1/65",
"6/1/66", "6/1/67", "6/1/68", "6/1/69", "6/1/70", "6/1/71", "6/1/72",
"6/1/73", "6/1/74", "6/1/75", "6/1/76", "6/1/77", "6/1/78", "6/1/79",
"6/1/80", "6/1/81", "6/1/82", "6/1/83", "6/1/84", "6/1/85", "6/1/86",
"6/1/87", "6/1/88", "6/1/89", "6/1/90", "6/1/91", "6/1/92", "6/1/93",
"6/1/94", "6/1/95", "6/1/96", "6/1/97", "6/1/98", "6/1/99", "7/1/00",
"7/1/01", "7/1/02", "7/1/03", "7/1/04", "7/1/05", "7/1/06", "7/1/07",
"7/1/08", "7/1/09", "7/1/10", "7/1/11", "7/1/12", "7/1/13", "7/1/14",
"7/1/15", "7/1/16", "7/1/17", "7/1/51", "7/1/52", "7/1/53", "7/1/54",
"7/1/55", "7/1/56", "7/1/57", "7/1/58", "7/1/59", "7/1/60", "7/1/61",
"7/1/62", "7/1/63", "7/1/64", "7/1/65", "7/1/66", "7/1/67", "7/1/68",
"7/1/69", "7/1/70", "7/1/71", "7/1/72", "7/1/73", "7/1/74", "7/1/75",
"7/1/76", "7/1/77", "7/1/78", "7/1/79", "7/1/80", "7/1/81", "7/1/82",
"7/1/83", "7/1/84", "7/1/85", "7/1/86", "7/1/87", "7/1/88", "7/1/89",
"7/1/90", "7/1/91", "7/1/92", "7/1/93", "7/1/94", "7/1/95", "7/1/96",
"7/1/97", "7/1/98", "7/1/99", "8/1/00", "8/1/01", "8/1/02", "8/1/03",
"8/1/04", "8/1/05", "8/1/06", "8/1/07", "8/1/08", "8/1/09", "8/1/10",
"8/1/11", "8/1/12", "8/1/13", "8/1/14", "8/1/15", "8/1/16", "8/1/17",
"8/1/51", "8/1/52", "8/1/53", "8/1/54", "8/1/55", "8/1/56", "8/1/57",
"8/1/58", "8/1/59", "8/1/60", "8/1/61", "8/1/62", "8/1/63", "8/1/64",
"8/1/65", "8/1/66", "8/1/67", "8/1/68", "8/1/69", "8/1/70", "8/1/71",
"8/1/72", "8/1/73", "8/1/74", "8/1/75", "8/1/76", "8/1/77", "8/1/78",
"8/1/79", "8/1/80", "8/1/81", "8/1/82", "8/1/83", "8/1/84", "8/1/85",
"8/1/86", "8/1/87", "8/1/88", "8/1/89", "8/1/90", "8/1/91", "8/1/92",
"8/1/93", "8/1/94", "8/1/95", "8/1/96", "8/1/97", "8/1/98", "8/1/99",
"9/1/00", "9/1/01", "9/1/02", "9/1/03", "9/1/04", "9/1/05", "9/1/06",
"9/1/07", "9/1/08", "9/1/09", "9/1/10", "9/1/11", "9/1/12", "9/1/13",
"9/1/14", "9/1/15", "9/1/16", "9/1/17", "9/1/51", "9/1/52", "9/1/53",
"9/1/54", "9/1/55", "9/1/56", "9/1/57", "9/1/58", "9/1/59", "9/1/60",
"9/1/61", "9/1/62", "9/1/63", "9/1/64", "9/1/65", "9/1/66", "9/1/67",
"9/1/68", "9/1/69", "9/1/70", "9/1/71", "9/1/72", "9/1/73", "9/1/74",
"9/1/75", "9/1/76", "9/1/77", "9/1/78", "9/1/79", "9/1/80", "9/1/81",
"9/1/82", "9/1/83", "9/1/84", "9/1/85", "9/1/86", "9/1/87", "9/1/88",
"9/1/89", "9/1/90", "9/1/91", "9/1/92", "9/1/93", "9/1/94", "9/1/95",
"9/1/96", "9/1/97", "9/1/98", "9/1/99"), class = "factor"), Calculated = c(5.493,
3.913, 4.477, 8.624, 23.624, 68.753, 83.529, 65.448, 34.033,
9.492, 8.131, 6.359, 4.752, 4.67, 5.044, 12.142, 18.306, 54.563,
57.81, 40.258, 19.823, 12.445, 9.493, 6.094, 5.041, 5.008, 5.059,
11.872, 28.777, 47.953, 79.965, 53.939, 32.667, 24.135, 10.508,
8.453, 4.132, 3.154, 3.669, 5.63, 18.806, 75.58, 55.823, 52.119,
36.837, 11.642, 6.833, 5.224, 4.642, 3.475, 5.642, 7.37, 25.816,
66.807, 76.858, 43.481, 36.17, 12.468, 7.03, 5.106, 3.768, 2.736,
3.161, 5.217, 28.561, 53.907, 83.948, 78.061, 64.183, 13.523,
6.877, 4.939, 3.869, 3.8, 6.76, 9.433, 17.835, 72.51, 67.519,
62.171, 23.953, 11.529, 7.68, 4.465, 3.651, 3.552, 3.274, 5.34,
47.831, 53.38, 70.997, 69.816, 38.313, 17.116, 8.423, 6.191,
4.821, 3.8, 4.933, 7.028, 24.206, 62.263, 81.155, 45.752, 30.903,
10.798, 5.604, 4.554, 3.746, 3.549, 4.108, 8.439, 32.208, 77.15,
55.642, 48.155, 43.367, 18, 10.163, 5.919, 5.009, 4.579, 5.466,
10.079, 22.463, 88.113, 68.523, 56.561, 36.04, 11.637, 5.385,
4.023, 3.371, 3.129, 3.116, 7.942, 18.046, 69.213, 67.461, 80.535,
35.167, 8.172, 6.034, 3.724, 2.874, 2.7, 3.033, 7.933, 25.466,
63.85, 74.716, 58.758, 33.26, 10.848, 7.263, 4.814, 4.29, 3.465,
3.234, 6.393, 27.759, 65.48, 63.739, 47.032, 29.303, 9.887, 6.892,
5.559, 4.194, 3.329, 3.665, 4.834, 24.532, 83.41, 86.613, 53.342,
43.073, 13.306, 5.755, 4.416, 3.667, 3.399, 3.427, 6.689, 30.119,
76.98, 61.184, 68.803, 31.273, 16.028, 9.403, 4.963, 4.649, 3.486,
4.175, 6.004, 27.423, 52.397, 98.11, 65.755, 30.583, 13.79, 7.064,
4.904, 4.384, 3.64, 4.049, 12.557, 23.481, 45.63, 65.594, 47.619,
27.317, 10.77, 7.129, 4.666, 3.997, 3.144, 3.351, 5.517, 29.513,
40.953, 66.203, 54.284, 24.827, 10.935, 6.293, 4.774, 4.409,
4.175, 4.054, 6.465, 14.014, 79.42, 75.019, 79.113, 48.123, 13.345,
7.81, 5.197, 4.755, 4.006, 3.783, 9.565, 30.397, 42.617, 72.806,
79.516, 26.427, 10.561, 7.684, 7.038, 4.653, 4.23, 4.445, 6.96,
17.237, 54.76, 80.723, 54.671, 17.506, 8.671, 7.998, 5.649, 4.76,
4.069, 3.834, 4.81, 31.742, 63.74, 63.942, 65.184, 36.6, 16.874,
8.564, 5.646, 5.019, 4.049, 5.839, 8.95, 18.149, 55.91, 86.377,
81.765, 41.1, 11.691, 7.006, 5.463, 4.325, 3.414, 3.56, 9.573,
35.661, 64.373, 107.71, 75.71, 45.653, 15.584, 8.779, 6.182,
3.965, 3.599, 3.633, 6.239, 21.255, 49.76, 62.481, 32.342, 28.367,
23.861, 11.512, 6.475, 5.142, 4.283, 6.856, 9.586, 39.052, 71.5,
76.939, 57.687, 35.827, 13.593, 7.738, 5.699, 3.994, 3.746, 4.044,
6.243, 16.876, 56.977, 70.058, 59.429, 24.34, 15.339, 7.305,
4.929, 4.548, 3.832, 4.522, 5.368, 34.489, 81.083, 66.981, 58.5,
37.103, 19.848, 10.095, 8.281, 6.166, 4.874, 4.747, 7.942, 19.321,
57.977, 67.9, 86.835, 34.11, 19.8, 9.314, 6.121, 4.605, 4.105,
7.091, 18.797, 26.744, 68.99, 69.755, 65.865, 38.007, 17.961,
10.386, 6.422, 5.047, 4.371, 4.113, 7.911, 27.282, 77.38, 92.916,
74.635, 49.973, 14.648, 7.872, 5.274, 4.482, 3.787, 5.129, 10.619,
29.277, 66.2, 98.071, 54.397, 36.307, 14.13, 6.54, 5.056, 3.908,
3.384, 3.605, 6.698, 18.272, 50.58, 69.913, 51.994, 30.863, 16.987,
7.556, 5.266, 3.947, 3.438, 3.2, 5.664, 24.588, 44.287, 93.174,
74.806, 34.483, 15.552, 6.219, 4.621, 3.815, 3.21, 3.615, 7.106,
50.452, 62.05, 61.452, 67.797, 30.723, 16.331, 6.057, 4.702,
3.755, 3.516, 3.452, 8.454, 19.271, 53.49, 107.129, 76.884, 52.54,
16.448, 7.681, 5.314, 4.36, 3.711, 3.222, 8.846, 35.532, 44.517,
87.406, 76.252, 31.65, 17.219, 7.366, 5.33, 4.437, 4.478, 5.502,
7.214, 25.796, 47.463, 89.816, 57.739, 23.49, 9.634, 6.196, 4.764,
3.942, 4.436, 5.978, 6.368, 34.59, 54.497, 74.261, 56.919, 19.2,
14.263, 6.474, 4.76, 4.03, 3.618, 4.961, 6.125, 9.667, 70.61,
91.026, 69.239, 41.577, 14.97, 6.582, 4.709, 3.771, 3.497, 3.929,
7.663, 43.294, 66.16, 78.945, 76.716, 31.43, 12.504, 8.98, 6.503,
4.535, 3.69, 4.223, 8.888, 38.761, 65.757, 71.306, 68.3, 28.46,
24.348, 10.773, 6.742, 4.616, 3.439, 6.086, 8.225, 31.065, 63.41,
92.042, 65.51, 37.91, 12.845, 7.809, 5.329, 4.01, 3.825, 3.73,
7.7, 29.823, 59.197, 110.006, 58.042, 27.02, 13.484, 6.963, 4.88,
4.099, 3.348, 3.615, 6.743, 23.323, 56.427, 54.877, 56.826, 19.983,
16.971, 12.01, 7.02, 5.006, 4.538, 5.486, 6.246, 29.462, 74.38,
66.445, 69.665, 40.42, 15.304, 8.366, 5.707, 4.627, 4.298, 4.34,
8.022, 27.329, 75.253, 78.932, 77.635, 34.553, 19.497, 11.177,
6.601, 4.712, 4.134, 5.148, 9.307, 48.826, 70.737, 103.61, 85.123,
49.903, 18.926, 9.709, 6.171, 4.889, 4.482, 4.961, 13.02, 52.945,
80.547, 60.69, 71.135, 32.417, 35.539, 16.113, 9.48, 6.441, 5.076,
6.436, 7.992, 45.913, 61.607, 90.465, 72.861, 26.057, 18.806,
8.243, 5.113, 4.147, 4.654, 7.135, 9.693, 38.784, 89.683, 70.948,
60.487, 27.707, 15.081, 17.073, 8.201, 4.862, 3.618, 4.085, 8.128,
38.261, 89.597, 71.594, 63.303, 18.943, 13.438, 9.913, 7.11,
4.937, 4.048, 4.808, 9.112, 19.352, 62.59, 86.574, 58.361, 29.883,
15.565, 15.158, 7.059, 5.025, 4.125, 5.256, 7.634, 28.503, 56.443,
59.89, 51.726, 30.123, 15.313, 7.835, 5.805, 5.075, 4.83, 5.088,
9.585, 34.874, 72.453, 77.594, 35.765, 34.877, 16.474, 7.121,
4.89, 4.047, 3.416, 3.938, 11.462, 25.806, 50.25, 54.977, 46.587,
24.333, 14.812, 7.309, 5.421, 4.055, 3.429, 3.608, 6.238, 39.858,
74.823, 71.519, 60.777, 31.43, 10.176, 8.437, 5.292, 3.901, 3.41,
3.802, 12.763, 48.823, 64.84, 80.352, 63.739, 28.483, 12.914,
7.363, 5.613, 4.252, 3.545, 3.879, 7.876, 26.877, 69.893, 69.655,
64.213, 28.523, 16.587, 10.579, 6.685, 5.368, 4.529, 4.807, 11.521,
32.194, 63.977, 53.287, 57.277, 39.35, 17.42, 8.038, 5.265, 4.328,
3.415, 5.85, 9.388, 30.742, 84.65, 74.161, 62.823, 30.897, 18.881,
12.891, 6.543, 5.115, 4.078, 4.263, 12.612, 26.029, 70.243, 81.474,
58.152, 28.94, 22.061, 13.861, 7.225, 5.293, 4.572, 6.028, 11.416,
29.374, 71.693, 77.087, 61.829, 31.387, 16.871, 14.46, 8.028,
5.973, 4.158, 4.287, 9.675, 36.49, 77.907, 81.697, 61.345, 27.84,
14.926, 9.018, 5.769, 4.178, 4.268, 4.225, 10.844, 23.024, 70,
69.39, 57.474, 28.953, 10.573, 7.251, 4.928, 3.899, 3.618, 5.025,
6.901, 29.605, 70.703, 60.384, 70.613, 33.297, 16.497, 9.272,
6.362)), class = "data.frame", row.names = c(NA, 804L))
这是一种方法
library(lubridate)
discharge$YYYY.MM.DD <- mdy(discharge$YYYY.MM.DD)
i1 <- year(discharge$YYYY.MM.DD) > 2050
year(discharge$YYYY.MM.DD[i1]) <- year(discharge$YYYY.MM.DD[i1]) - 100
-输出
> head(discharge)
YYYY.MM.DD Calculated
1 1951-01-01 5.493
2 1951-02-01 3.913
3 1951-03-01 4.477
4 1951-04-01 8.624
5 1951-05-01 23.624
6 1951-06-01 68.753
> tail(discharge)
YYYY.MM.DD Calculated
799 2017-07-01 60.384
800 2017-08-01 70.613
801 2017-09-01 33.297
802 2017-10-01 16.497
803 2017-11-01 9.272
804 2017-12-01 6.362
这是另一种方式:
library(lubridate)
parse_date_time2(df$YYYY.MM.DD, "mdy", cutoff_2000 = 50)```
这是基本的 R 方式。
- 取后两位,强制为整数;
- 小于51加2000,大于51加1900;
- 提取字符串直到最后 2 位数字;
- 将 day/month 和 2 位年份粘贴在一起并强制转换为 class
"Date"
.
y <- as.integer(substring(discharge$YYYY.MM.DD, nchar(as.character(discharge$YYYY.MM.DD)) - 1L))
y <- ifelse(y < 51, 2000 + y, 1900 + y)
d <- sub("(^.*)/\d{2}$", "\1", discharge$YYYY.MM.DD)
discharge$YYYY.MM.DD <- as.Date(paste(d, y, sep = "/"), "%m/%d/%Y")
rm(y, d)
head(discharge)
# YYYY.MM.DD Calculated
#1 1951-01-01 5.493
#2 1951-02-01 3.913
#3 1951-03-01 4.477
#4 1951-04-01 8.624
#5 1951-05-01 23.624
#6 1951-06-01 68.753
我有一个日期从 1950 年代到 2017 年的数据集。我正在尝试将格式转换为 %m/%d/%Y
,但在指定 2000 年代到 2010 年代不同的日期时我被绊倒了。
library(tidyverse)
library(ggplot2)
discharge <- discharge %>%
mutate(YYYY.MM.DD = sub("^(\d+/\d+/)(\d+)$", "\119\2", YYYY.MM.DD)) %>%
mutate(YYYY.MM.DD = sub("190","200","201", YYYY.MM.DD)) %>%
mutate(YYYY.MM.DD = strptime(YYYY.MM.DD, format = "%m/%d/%Y"))
这会输出 NA,而删除“201”或“200”只会使值再次循环到 19 世纪。如有任何帮助,我们将不胜感激!
> dput(discharge)
structure(list(YYYY.MM.DD = structure(c(19L, 287L, 354L, 421L,
488L, 555L, 622L, 689L, 756L, 86L, 153L, 220L, 20L, 288L, 355L,
422L, 489L, 556L, 623L, 690L, 757L, 87L, 154L, 221L, 21L, 289L,
356L, 423L, 490L, 557L, 624L, 691L, 758L, 88L, 155L, 222L, 22L,
290L, 357L, 424L, 491L, 558L, 625L, 692L, 759L, 89L, 156L, 223L,
23L, 291L, 358L, 425L, 492L, 559L, 626L, 693L, 760L, 90L, 157L,
224L, 24L, 292L, 359L, 426L, 493L, 560L, 627L, 694L, 761L, 91L,
158L, 225L, 25L, 293L, 360L, 427L, 494L, 561L, 628L, 695L, 762L,
92L, 159L, 226L, 26L, 294L, 361L, 428L, 495L, 562L, 629L, 696L,
763L, 93L, 160L, 227L, 27L, 295L, 362L, 429L, 496L, 563L, 630L,
697L, 764L, 94L, 161L, 228L, 28L, 296L, 363L, 430L, 497L, 564L,
631L, 698L, 765L, 95L, 162L, 229L, 29L, 297L, 364L, 431L, 498L,
565L, 632L, 699L, 766L, 96L, 163L, 230L, 30L, 298L, 365L, 432L,
499L, 566L, 633L, 700L, 767L, 97L, 164L, 231L, 31L, 299L, 366L,
433L, 500L, 567L, 634L, 701L, 768L, 98L, 165L, 232L, 32L, 300L,
367L, 434L, 501L, 568L, 635L, 702L, 769L, 99L, 166L, 233L, 33L,
301L, 368L, 435L, 502L, 569L, 636L, 703L, 770L, 100L, 167L, 234L,
34L, 302L, 369L, 436L, 503L, 570L, 637L, 704L, 771L, 101L, 168L,
235L, 35L, 303L, 370L, 437L, 504L, 571L, 638L, 705L, 772L, 102L,
169L, 236L, 36L, 304L, 371L, 438L, 505L, 572L, 639L, 706L, 773L,
103L, 170L, 237L, 37L, 305L, 372L, 439L, 506L, 573L, 640L, 707L,
774L, 104L, 171L, 238L, 38L, 306L, 373L, 440L, 507L, 574L, 641L,
708L, 775L, 105L, 172L, 239L, 39L, 307L, 374L, 441L, 508L, 575L,
642L, 709L, 776L, 106L, 173L, 240L, 40L, 308L, 375L, 442L, 509L,
576L, 643L, 710L, 777L, 107L, 174L, 241L, 41L, 309L, 376L, 443L,
510L, 577L, 644L, 711L, 778L, 108L, 175L, 242L, 42L, 310L, 377L,
444L, 511L, 578L, 645L, 712L, 779L, 109L, 176L, 243L, 43L, 311L,
378L, 445L, 512L, 579L, 646L, 713L, 780L, 110L, 177L, 244L, 44L,
312L, 379L, 446L, 513L, 580L, 647L, 714L, 781L, 111L, 178L, 245L,
45L, 313L, 380L, 447L, 514L, 581L, 648L, 715L, 782L, 112L, 179L,
246L, 46L, 314L, 381L, 448L, 515L, 582L, 649L, 716L, 783L, 113L,
180L, 247L, 47L, 315L, 382L, 449L, 516L, 583L, 650L, 717L, 784L,
114L, 181L, 248L, 48L, 316L, 383L, 450L, 517L, 584L, 651L, 718L,
785L, 115L, 182L, 249L, 49L, 317L, 384L, 451L, 518L, 585L, 652L,
719L, 786L, 116L, 183L, 250L, 50L, 318L, 385L, 452L, 519L, 586L,
653L, 720L, 787L, 117L, 184L, 251L, 51L, 319L, 386L, 453L, 520L,
587L, 654L, 721L, 788L, 118L, 185L, 252L, 52L, 320L, 387L, 454L,
521L, 588L, 655L, 722L, 789L, 119L, 186L, 253L, 53L, 321L, 388L,
455L, 522L, 589L, 656L, 723L, 790L, 120L, 187L, 254L, 54L, 322L,
389L, 456L, 523L, 590L, 657L, 724L, 791L, 121L, 188L, 255L, 55L,
323L, 390L, 457L, 524L, 591L, 658L, 725L, 792L, 122L, 189L, 256L,
56L, 324L, 391L, 458L, 525L, 592L, 659L, 726L, 793L, 123L, 190L,
257L, 57L, 325L, 392L, 459L, 526L, 593L, 660L, 727L, 794L, 124L,
191L, 258L, 58L, 326L, 393L, 460L, 527L, 594L, 661L, 728L, 795L,
125L, 192L, 259L, 59L, 327L, 394L, 461L, 528L, 595L, 662L, 729L,
796L, 126L, 193L, 260L, 60L, 328L, 395L, 462L, 529L, 596L, 663L,
730L, 797L, 127L, 194L, 261L, 61L, 329L, 396L, 463L, 530L, 597L,
664L, 731L, 798L, 128L, 195L, 262L, 62L, 330L, 397L, 464L, 531L,
598L, 665L, 732L, 799L, 129L, 196L, 263L, 63L, 331L, 398L, 465L,
532L, 599L, 666L, 733L, 800L, 130L, 197L, 264L, 64L, 332L, 399L,
466L, 533L, 600L, 667L, 734L, 801L, 131L, 198L, 265L, 65L, 333L,
400L, 467L, 534L, 601L, 668L, 735L, 802L, 132L, 199L, 266L, 66L,
334L, 401L, 468L, 535L, 602L, 669L, 736L, 803L, 133L, 200L, 267L,
67L, 335L, 402L, 469L, 536L, 603L, 670L, 737L, 804L, 134L, 201L,
268L, 1L, 269L, 336L, 403L, 470L, 537L, 604L, 671L, 738L, 68L,
135L, 202L, 2L, 270L, 337L, 404L, 471L, 538L, 605L, 672L, 739L,
69L, 136L, 203L, 3L, 271L, 338L, 405L, 472L, 539L, 606L, 673L,
740L, 70L, 137L, 204L, 4L, 272L, 339L, 406L, 473L, 540L, 607L,
674L, 741L, 71L, 138L, 205L, 5L, 273L, 340L, 407L, 474L, 541L,
608L, 675L, 742L, 72L, 139L, 206L, 6L, 274L, 341L, 408L, 475L,
542L, 609L, 676L, 743L, 73L, 140L, 207L, 7L, 275L, 342L, 409L,
476L, 543L, 610L, 677L, 744L, 74L, 141L, 208L, 8L, 276L, 343L,
410L, 477L, 544L, 611L, 678L, 745L, 75L, 142L, 209L, 9L, 277L,
344L, 411L, 478L, 545L, 612L, 679L, 746L, 76L, 143L, 210L, 10L,
278L, 345L, 412L, 479L, 546L, 613L, 680L, 747L, 77L, 144L, 211L,
11L, 279L, 346L, 413L, 480L, 547L, 614L, 681L, 748L, 78L, 145L,
212L, 12L, 280L, 347L, 414L, 481L, 548L, 615L, 682L, 749L, 79L,
146L, 213L, 13L, 281L, 348L, 415L, 482L, 549L, 616L, 683L, 750L,
80L, 147L, 214L, 14L, 282L, 349L, 416L, 483L, 550L, 617L, 684L,
751L, 81L, 148L, 215L, 15L, 283L, 350L, 417L, 484L, 551L, 618L,
685L, 752L, 82L, 149L, 216L, 16L, 284L, 351L, 418L, 485L, 552L,
619L, 686L, 753L, 83L, 150L, 217L, 17L, 285L, 352L, 419L, 486L,
553L, 620L, 687L, 754L, 84L, 151L, 218L, 18L, 286L, 353L, 420L,
487L, 554L, 621L, 688L, 755L, 85L, 152L, 219L), .Label = c("1/1/00",
"1/1/01", "1/1/02", "1/1/03", "1/1/04", "1/1/05", "1/1/06", "1/1/07",
"1/1/08", "1/1/09", "1/1/10", "1/1/11", "1/1/12", "1/1/13", "1/1/14",
"1/1/15", "1/1/16", "1/1/17", "1/1/51", "1/1/52", "1/1/53", "1/1/54",
"1/1/55", "1/1/56", "1/1/57", "1/1/58", "1/1/59", "1/1/60", "1/1/61",
"1/1/62", "1/1/63", "1/1/64", "1/1/65", "1/1/66", "1/1/67", "1/1/68",
"1/1/69", "1/1/70", "1/1/71", "1/1/72", "1/1/73", "1/1/74", "1/1/75",
"1/1/76", "1/1/77", "1/1/78", "1/1/79", "1/1/80", "1/1/81", "1/1/82",
"1/1/83", "1/1/84", "1/1/85", "1/1/86", "1/1/87", "1/1/88", "1/1/89",
"1/1/90", "1/1/91", "1/1/92", "1/1/93", "1/1/94", "1/1/95", "1/1/96",
"1/1/97", "1/1/98", "1/1/99", "10/1/00", "10/1/01", "10/1/02",
"10/1/03", "10/1/04", "10/1/05", "10/1/06", "10/1/07", "10/1/08",
"10/1/09", "10/1/10", "10/1/11", "10/1/12", "10/1/13", "10/1/14",
"10/1/15", "10/1/16", "10/1/17", "10/1/51", "10/1/52", "10/1/53",
"10/1/54", "10/1/55", "10/1/56", "10/1/57", "10/1/58", "10/1/59",
"10/1/60", "10/1/61", "10/1/62", "10/1/63", "10/1/64", "10/1/65",
"10/1/66", "10/1/67", "10/1/68", "10/1/69", "10/1/70", "10/1/71",
"10/1/72", "10/1/73", "10/1/74", "10/1/75", "10/1/76", "10/1/77",
"10/1/78", "10/1/79", "10/1/80", "10/1/81", "10/1/82", "10/1/83",
"10/1/84", "10/1/85", "10/1/86", "10/1/87", "10/1/88", "10/1/89",
"10/1/90", "10/1/91", "10/1/92", "10/1/93", "10/1/94", "10/1/95",
"10/1/96", "10/1/97", "10/1/98", "10/1/99", "11/1/00", "11/1/01",
"11/1/02", "11/1/03", "11/1/04", "11/1/05", "11/1/06", "11/1/07",
"11/1/08", "11/1/09", "11/1/10", "11/1/11", "11/1/12", "11/1/13",
"11/1/14", "11/1/15", "11/1/16", "11/1/17", "11/1/51", "11/1/52",
"11/1/53", "11/1/54", "11/1/55", "11/1/56", "11/1/57", "11/1/58",
"11/1/59", "11/1/60", "11/1/61", "11/1/62", "11/1/63", "11/1/64",
"11/1/65", "11/1/66", "11/1/67", "11/1/68", "11/1/69", "11/1/70",
"11/1/71", "11/1/72", "11/1/73", "11/1/74", "11/1/75", "11/1/76",
"11/1/77", "11/1/78", "11/1/79", "11/1/80", "11/1/81", "11/1/82",
"11/1/83", "11/1/84", "11/1/85", "11/1/86", "11/1/87", "11/1/88",
"11/1/89", "11/1/90", "11/1/91", "11/1/92", "11/1/93", "11/1/94",
"11/1/95", "11/1/96", "11/1/97", "11/1/98", "11/1/99", "12/1/00",
"12/1/01", "12/1/02", "12/1/03", "12/1/04", "12/1/05", "12/1/06",
"12/1/07", "12/1/08", "12/1/09", "12/1/10", "12/1/11", "12/1/12",
"12/1/13", "12/1/14", "12/1/15", "12/1/16", "12/1/17", "12/1/51",
"12/1/52", "12/1/53", "12/1/54", "12/1/55", "12/1/56", "12/1/57",
"12/1/58", "12/1/59", "12/1/60", "12/1/61", "12/1/62", "12/1/63",
"12/1/64", "12/1/65", "12/1/66", "12/1/67", "12/1/68", "12/1/69",
"12/1/70", "12/1/71", "12/1/72", "12/1/73", "12/1/74", "12/1/75",
"12/1/76", "12/1/77", "12/1/78", "12/1/79", "12/1/80", "12/1/81",
"12/1/82", "12/1/83", "12/1/84", "12/1/85", "12/1/86", "12/1/87",
"12/1/88", "12/1/89", "12/1/90", "12/1/91", "12/1/92", "12/1/93",
"12/1/94", "12/1/95", "12/1/96", "12/1/97", "12/1/98", "12/1/99",
"2/1/00", "2/1/01", "2/1/02", "2/1/03", "2/1/04", "2/1/05", "2/1/06",
"2/1/07", "2/1/08", "2/1/09", "2/1/10", "2/1/11", "2/1/12", "2/1/13",
"2/1/14", "2/1/15", "2/1/16", "2/1/17", "2/1/51", "2/1/52", "2/1/53",
"2/1/54", "2/1/55", "2/1/56", "2/1/57", "2/1/58", "2/1/59", "2/1/60",
"2/1/61", "2/1/62", "2/1/63", "2/1/64", "2/1/65", "2/1/66", "2/1/67",
"2/1/68", "2/1/69", "2/1/70", "2/1/71", "2/1/72", "2/1/73", "2/1/74",
"2/1/75", "2/1/76", "2/1/77", "2/1/78", "2/1/79", "2/1/80", "2/1/81",
"2/1/82", "2/1/83", "2/1/84", "2/1/85", "2/1/86", "2/1/87", "2/1/88",
"2/1/89", "2/1/90", "2/1/91", "2/1/92", "2/1/93", "2/1/94", "2/1/95",
"2/1/96", "2/1/97", "2/1/98", "2/1/99", "3/1/00", "3/1/01", "3/1/02",
"3/1/03", "3/1/04", "3/1/05", "3/1/06", "3/1/07", "3/1/08", "3/1/09",
"3/1/10", "3/1/11", "3/1/12", "3/1/13", "3/1/14", "3/1/15", "3/1/16",
"3/1/17", "3/1/51", "3/1/52", "3/1/53", "3/1/54", "3/1/55", "3/1/56",
"3/1/57", "3/1/58", "3/1/59", "3/1/60", "3/1/61", "3/1/62", "3/1/63",
"3/1/64", "3/1/65", "3/1/66", "3/1/67", "3/1/68", "3/1/69", "3/1/70",
"3/1/71", "3/1/72", "3/1/73", "3/1/74", "3/1/75", "3/1/76", "3/1/77",
"3/1/78", "3/1/79", "3/1/80", "3/1/81", "3/1/82", "3/1/83", "3/1/84",
"3/1/85", "3/1/86", "3/1/87", "3/1/88", "3/1/89", "3/1/90", "3/1/91",
"3/1/92", "3/1/93", "3/1/94", "3/1/95", "3/1/96", "3/1/97", "3/1/98",
"3/1/99", "4/1/00", "4/1/01", "4/1/02", "4/1/03", "4/1/04", "4/1/05",
"4/1/06", "4/1/07", "4/1/08", "4/1/09", "4/1/10", "4/1/11", "4/1/12",
"4/1/13", "4/1/14", "4/1/15", "4/1/16", "4/1/17", "4/1/51", "4/1/52",
"4/1/53", "4/1/54", "4/1/55", "4/1/56", "4/1/57", "4/1/58", "4/1/59",
"4/1/60", "4/1/61", "4/1/62", "4/1/63", "4/1/64", "4/1/65", "4/1/66",
"4/1/67", "4/1/68", "4/1/69", "4/1/70", "4/1/71", "4/1/72", "4/1/73",
"4/1/74", "4/1/75", "4/1/76", "4/1/77", "4/1/78", "4/1/79", "4/1/80",
"4/1/81", "4/1/82", "4/1/83", "4/1/84", "4/1/85", "4/1/86", "4/1/87",
"4/1/88", "4/1/89", "4/1/90", "4/1/91", "4/1/92", "4/1/93", "4/1/94",
"4/1/95", "4/1/96", "4/1/97", "4/1/98", "4/1/99", "5/1/00", "5/1/01",
"5/1/02", "5/1/03", "5/1/04", "5/1/05", "5/1/06", "5/1/07", "5/1/08",
"5/1/09", "5/1/10", "5/1/11", "5/1/12", "5/1/13", "5/1/14", "5/1/15",
"5/1/16", "5/1/17", "5/1/51", "5/1/52", "5/1/53", "5/1/54", "5/1/55",
"5/1/56", "5/1/57", "5/1/58", "5/1/59", "5/1/60", "5/1/61", "5/1/62",
"5/1/63", "5/1/64", "5/1/65", "5/1/66", "5/1/67", "5/1/68", "5/1/69",
"5/1/70", "5/1/71", "5/1/72", "5/1/73", "5/1/74", "5/1/75", "5/1/76",
"5/1/77", "5/1/78", "5/1/79", "5/1/80", "5/1/81", "5/1/82", "5/1/83",
"5/1/84", "5/1/85", "5/1/86", "5/1/87", "5/1/88", "5/1/89", "5/1/90",
"5/1/91", "5/1/92", "5/1/93", "5/1/94", "5/1/95", "5/1/96", "5/1/97",
"5/1/98", "5/1/99", "6/1/00", "6/1/01", "6/1/02", "6/1/03", "6/1/04",
"6/1/05", "6/1/06", "6/1/07", "6/1/08", "6/1/09", "6/1/10", "6/1/11",
"6/1/12", "6/1/13", "6/1/14", "6/1/15", "6/1/16", "6/1/17", "6/1/51",
"6/1/52", "6/1/53", "6/1/54", "6/1/55", "6/1/56", "6/1/57", "6/1/58",
"6/1/59", "6/1/60", "6/1/61", "6/1/62", "6/1/63", "6/1/64", "6/1/65",
"6/1/66", "6/1/67", "6/1/68", "6/1/69", "6/1/70", "6/1/71", "6/1/72",
"6/1/73", "6/1/74", "6/1/75", "6/1/76", "6/1/77", "6/1/78", "6/1/79",
"6/1/80", "6/1/81", "6/1/82", "6/1/83", "6/1/84", "6/1/85", "6/1/86",
"6/1/87", "6/1/88", "6/1/89", "6/1/90", "6/1/91", "6/1/92", "6/1/93",
"6/1/94", "6/1/95", "6/1/96", "6/1/97", "6/1/98", "6/1/99", "7/1/00",
"7/1/01", "7/1/02", "7/1/03", "7/1/04", "7/1/05", "7/1/06", "7/1/07",
"7/1/08", "7/1/09", "7/1/10", "7/1/11", "7/1/12", "7/1/13", "7/1/14",
"7/1/15", "7/1/16", "7/1/17", "7/1/51", "7/1/52", "7/1/53", "7/1/54",
"7/1/55", "7/1/56", "7/1/57", "7/1/58", "7/1/59", "7/1/60", "7/1/61",
"7/1/62", "7/1/63", "7/1/64", "7/1/65", "7/1/66", "7/1/67", "7/1/68",
"7/1/69", "7/1/70", "7/1/71", "7/1/72", "7/1/73", "7/1/74", "7/1/75",
"7/1/76", "7/1/77", "7/1/78", "7/1/79", "7/1/80", "7/1/81", "7/1/82",
"7/1/83", "7/1/84", "7/1/85", "7/1/86", "7/1/87", "7/1/88", "7/1/89",
"7/1/90", "7/1/91", "7/1/92", "7/1/93", "7/1/94", "7/1/95", "7/1/96",
"7/1/97", "7/1/98", "7/1/99", "8/1/00", "8/1/01", "8/1/02", "8/1/03",
"8/1/04", "8/1/05", "8/1/06", "8/1/07", "8/1/08", "8/1/09", "8/1/10",
"8/1/11", "8/1/12", "8/1/13", "8/1/14", "8/1/15", "8/1/16", "8/1/17",
"8/1/51", "8/1/52", "8/1/53", "8/1/54", "8/1/55", "8/1/56", "8/1/57",
"8/1/58", "8/1/59", "8/1/60", "8/1/61", "8/1/62", "8/1/63", "8/1/64",
"8/1/65", "8/1/66", "8/1/67", "8/1/68", "8/1/69", "8/1/70", "8/1/71",
"8/1/72", "8/1/73", "8/1/74", "8/1/75", "8/1/76", "8/1/77", "8/1/78",
"8/1/79", "8/1/80", "8/1/81", "8/1/82", "8/1/83", "8/1/84", "8/1/85",
"8/1/86", "8/1/87", "8/1/88", "8/1/89", "8/1/90", "8/1/91", "8/1/92",
"8/1/93", "8/1/94", "8/1/95", "8/1/96", "8/1/97", "8/1/98", "8/1/99",
"9/1/00", "9/1/01", "9/1/02", "9/1/03", "9/1/04", "9/1/05", "9/1/06",
"9/1/07", "9/1/08", "9/1/09", "9/1/10", "9/1/11", "9/1/12", "9/1/13",
"9/1/14", "9/1/15", "9/1/16", "9/1/17", "9/1/51", "9/1/52", "9/1/53",
"9/1/54", "9/1/55", "9/1/56", "9/1/57", "9/1/58", "9/1/59", "9/1/60",
"9/1/61", "9/1/62", "9/1/63", "9/1/64", "9/1/65", "9/1/66", "9/1/67",
"9/1/68", "9/1/69", "9/1/70", "9/1/71", "9/1/72", "9/1/73", "9/1/74",
"9/1/75", "9/1/76", "9/1/77", "9/1/78", "9/1/79", "9/1/80", "9/1/81",
"9/1/82", "9/1/83", "9/1/84", "9/1/85", "9/1/86", "9/1/87", "9/1/88",
"9/1/89", "9/1/90", "9/1/91", "9/1/92", "9/1/93", "9/1/94", "9/1/95",
"9/1/96", "9/1/97", "9/1/98", "9/1/99"), class = "factor"), Calculated = c(5.493,
3.913, 4.477, 8.624, 23.624, 68.753, 83.529, 65.448, 34.033,
9.492, 8.131, 6.359, 4.752, 4.67, 5.044, 12.142, 18.306, 54.563,
57.81, 40.258, 19.823, 12.445, 9.493, 6.094, 5.041, 5.008, 5.059,
11.872, 28.777, 47.953, 79.965, 53.939, 32.667, 24.135, 10.508,
8.453, 4.132, 3.154, 3.669, 5.63, 18.806, 75.58, 55.823, 52.119,
36.837, 11.642, 6.833, 5.224, 4.642, 3.475, 5.642, 7.37, 25.816,
66.807, 76.858, 43.481, 36.17, 12.468, 7.03, 5.106, 3.768, 2.736,
3.161, 5.217, 28.561, 53.907, 83.948, 78.061, 64.183, 13.523,
6.877, 4.939, 3.869, 3.8, 6.76, 9.433, 17.835, 72.51, 67.519,
62.171, 23.953, 11.529, 7.68, 4.465, 3.651, 3.552, 3.274, 5.34,
47.831, 53.38, 70.997, 69.816, 38.313, 17.116, 8.423, 6.191,
4.821, 3.8, 4.933, 7.028, 24.206, 62.263, 81.155, 45.752, 30.903,
10.798, 5.604, 4.554, 3.746, 3.549, 4.108, 8.439, 32.208, 77.15,
55.642, 48.155, 43.367, 18, 10.163, 5.919, 5.009, 4.579, 5.466,
10.079, 22.463, 88.113, 68.523, 56.561, 36.04, 11.637, 5.385,
4.023, 3.371, 3.129, 3.116, 7.942, 18.046, 69.213, 67.461, 80.535,
35.167, 8.172, 6.034, 3.724, 2.874, 2.7, 3.033, 7.933, 25.466,
63.85, 74.716, 58.758, 33.26, 10.848, 7.263, 4.814, 4.29, 3.465,
3.234, 6.393, 27.759, 65.48, 63.739, 47.032, 29.303, 9.887, 6.892,
5.559, 4.194, 3.329, 3.665, 4.834, 24.532, 83.41, 86.613, 53.342,
43.073, 13.306, 5.755, 4.416, 3.667, 3.399, 3.427, 6.689, 30.119,
76.98, 61.184, 68.803, 31.273, 16.028, 9.403, 4.963, 4.649, 3.486,
4.175, 6.004, 27.423, 52.397, 98.11, 65.755, 30.583, 13.79, 7.064,
4.904, 4.384, 3.64, 4.049, 12.557, 23.481, 45.63, 65.594, 47.619,
27.317, 10.77, 7.129, 4.666, 3.997, 3.144, 3.351, 5.517, 29.513,
40.953, 66.203, 54.284, 24.827, 10.935, 6.293, 4.774, 4.409,
4.175, 4.054, 6.465, 14.014, 79.42, 75.019, 79.113, 48.123, 13.345,
7.81, 5.197, 4.755, 4.006, 3.783, 9.565, 30.397, 42.617, 72.806,
79.516, 26.427, 10.561, 7.684, 7.038, 4.653, 4.23, 4.445, 6.96,
17.237, 54.76, 80.723, 54.671, 17.506, 8.671, 7.998, 5.649, 4.76,
4.069, 3.834, 4.81, 31.742, 63.74, 63.942, 65.184, 36.6, 16.874,
8.564, 5.646, 5.019, 4.049, 5.839, 8.95, 18.149, 55.91, 86.377,
81.765, 41.1, 11.691, 7.006, 5.463, 4.325, 3.414, 3.56, 9.573,
35.661, 64.373, 107.71, 75.71, 45.653, 15.584, 8.779, 6.182,
3.965, 3.599, 3.633, 6.239, 21.255, 49.76, 62.481, 32.342, 28.367,
23.861, 11.512, 6.475, 5.142, 4.283, 6.856, 9.586, 39.052, 71.5,
76.939, 57.687, 35.827, 13.593, 7.738, 5.699, 3.994, 3.746, 4.044,
6.243, 16.876, 56.977, 70.058, 59.429, 24.34, 15.339, 7.305,
4.929, 4.548, 3.832, 4.522, 5.368, 34.489, 81.083, 66.981, 58.5,
37.103, 19.848, 10.095, 8.281, 6.166, 4.874, 4.747, 7.942, 19.321,
57.977, 67.9, 86.835, 34.11, 19.8, 9.314, 6.121, 4.605, 4.105,
7.091, 18.797, 26.744, 68.99, 69.755, 65.865, 38.007, 17.961,
10.386, 6.422, 5.047, 4.371, 4.113, 7.911, 27.282, 77.38, 92.916,
74.635, 49.973, 14.648, 7.872, 5.274, 4.482, 3.787, 5.129, 10.619,
29.277, 66.2, 98.071, 54.397, 36.307, 14.13, 6.54, 5.056, 3.908,
3.384, 3.605, 6.698, 18.272, 50.58, 69.913, 51.994, 30.863, 16.987,
7.556, 5.266, 3.947, 3.438, 3.2, 5.664, 24.588, 44.287, 93.174,
74.806, 34.483, 15.552, 6.219, 4.621, 3.815, 3.21, 3.615, 7.106,
50.452, 62.05, 61.452, 67.797, 30.723, 16.331, 6.057, 4.702,
3.755, 3.516, 3.452, 8.454, 19.271, 53.49, 107.129, 76.884, 52.54,
16.448, 7.681, 5.314, 4.36, 3.711, 3.222, 8.846, 35.532, 44.517,
87.406, 76.252, 31.65, 17.219, 7.366, 5.33, 4.437, 4.478, 5.502,
7.214, 25.796, 47.463, 89.816, 57.739, 23.49, 9.634, 6.196, 4.764,
3.942, 4.436, 5.978, 6.368, 34.59, 54.497, 74.261, 56.919, 19.2,
14.263, 6.474, 4.76, 4.03, 3.618, 4.961, 6.125, 9.667, 70.61,
91.026, 69.239, 41.577, 14.97, 6.582, 4.709, 3.771, 3.497, 3.929,
7.663, 43.294, 66.16, 78.945, 76.716, 31.43, 12.504, 8.98, 6.503,
4.535, 3.69, 4.223, 8.888, 38.761, 65.757, 71.306, 68.3, 28.46,
24.348, 10.773, 6.742, 4.616, 3.439, 6.086, 8.225, 31.065, 63.41,
92.042, 65.51, 37.91, 12.845, 7.809, 5.329, 4.01, 3.825, 3.73,
7.7, 29.823, 59.197, 110.006, 58.042, 27.02, 13.484, 6.963, 4.88,
4.099, 3.348, 3.615, 6.743, 23.323, 56.427, 54.877, 56.826, 19.983,
16.971, 12.01, 7.02, 5.006, 4.538, 5.486, 6.246, 29.462, 74.38,
66.445, 69.665, 40.42, 15.304, 8.366, 5.707, 4.627, 4.298, 4.34,
8.022, 27.329, 75.253, 78.932, 77.635, 34.553, 19.497, 11.177,
6.601, 4.712, 4.134, 5.148, 9.307, 48.826, 70.737, 103.61, 85.123,
49.903, 18.926, 9.709, 6.171, 4.889, 4.482, 4.961, 13.02, 52.945,
80.547, 60.69, 71.135, 32.417, 35.539, 16.113, 9.48, 6.441, 5.076,
6.436, 7.992, 45.913, 61.607, 90.465, 72.861, 26.057, 18.806,
8.243, 5.113, 4.147, 4.654, 7.135, 9.693, 38.784, 89.683, 70.948,
60.487, 27.707, 15.081, 17.073, 8.201, 4.862, 3.618, 4.085, 8.128,
38.261, 89.597, 71.594, 63.303, 18.943, 13.438, 9.913, 7.11,
4.937, 4.048, 4.808, 9.112, 19.352, 62.59, 86.574, 58.361, 29.883,
15.565, 15.158, 7.059, 5.025, 4.125, 5.256, 7.634, 28.503, 56.443,
59.89, 51.726, 30.123, 15.313, 7.835, 5.805, 5.075, 4.83, 5.088,
9.585, 34.874, 72.453, 77.594, 35.765, 34.877, 16.474, 7.121,
4.89, 4.047, 3.416, 3.938, 11.462, 25.806, 50.25, 54.977, 46.587,
24.333, 14.812, 7.309, 5.421, 4.055, 3.429, 3.608, 6.238, 39.858,
74.823, 71.519, 60.777, 31.43, 10.176, 8.437, 5.292, 3.901, 3.41,
3.802, 12.763, 48.823, 64.84, 80.352, 63.739, 28.483, 12.914,
7.363, 5.613, 4.252, 3.545, 3.879, 7.876, 26.877, 69.893, 69.655,
64.213, 28.523, 16.587, 10.579, 6.685, 5.368, 4.529, 4.807, 11.521,
32.194, 63.977, 53.287, 57.277, 39.35, 17.42, 8.038, 5.265, 4.328,
3.415, 5.85, 9.388, 30.742, 84.65, 74.161, 62.823, 30.897, 18.881,
12.891, 6.543, 5.115, 4.078, 4.263, 12.612, 26.029, 70.243, 81.474,
58.152, 28.94, 22.061, 13.861, 7.225, 5.293, 4.572, 6.028, 11.416,
29.374, 71.693, 77.087, 61.829, 31.387, 16.871, 14.46, 8.028,
5.973, 4.158, 4.287, 9.675, 36.49, 77.907, 81.697, 61.345, 27.84,
14.926, 9.018, 5.769, 4.178, 4.268, 4.225, 10.844, 23.024, 70,
69.39, 57.474, 28.953, 10.573, 7.251, 4.928, 3.899, 3.618, 5.025,
6.901, 29.605, 70.703, 60.384, 70.613, 33.297, 16.497, 9.272,
6.362)), class = "data.frame", row.names = c(NA, 804L))
这是一种方法
library(lubridate)
discharge$YYYY.MM.DD <- mdy(discharge$YYYY.MM.DD)
i1 <- year(discharge$YYYY.MM.DD) > 2050
year(discharge$YYYY.MM.DD[i1]) <- year(discharge$YYYY.MM.DD[i1]) - 100
-输出
> head(discharge)
YYYY.MM.DD Calculated
1 1951-01-01 5.493
2 1951-02-01 3.913
3 1951-03-01 4.477
4 1951-04-01 8.624
5 1951-05-01 23.624
6 1951-06-01 68.753
> tail(discharge)
YYYY.MM.DD Calculated
799 2017-07-01 60.384
800 2017-08-01 70.613
801 2017-09-01 33.297
802 2017-10-01 16.497
803 2017-11-01 9.272
804 2017-12-01 6.362
这是另一种方式:
library(lubridate)
parse_date_time2(df$YYYY.MM.DD, "mdy", cutoff_2000 = 50)```
这是基本的 R 方式。
- 取后两位,强制为整数;
- 小于51加2000,大于51加1900;
- 提取字符串直到最后 2 位数字;
- 将 day/month 和 2 位年份粘贴在一起并强制转换为 class
"Date"
.
y <- as.integer(substring(discharge$YYYY.MM.DD, nchar(as.character(discharge$YYYY.MM.DD)) - 1L))
y <- ifelse(y < 51, 2000 + y, 1900 + y)
d <- sub("(^.*)/\d{2}$", "\1", discharge$YYYY.MM.DD)
discharge$YYYY.MM.DD <- as.Date(paste(d, y, sep = "/"), "%m/%d/%Y")
rm(y, d)
head(discharge)
# YYYY.MM.DD Calculated
#1 1951-01-01 5.493
#2 1951-02-01 3.913
#3 1951-03-01 4.477
#4 1951-04-01 8.624
#5 1951-05-01 23.624
#6 1951-06-01 68.753