as.Date with two-digit years
x = format(as.Date("10.10.61", "%d.%m.%y"), "19%y-%m-%d")
x = as.Date(x)
x
class(x)
Reading Dates with two digit years in R
The zoo package is helpful for this:
require(zoo)
x <- as.yearmon("Jan-84", format = "%b-%y")
The lines above first convert the input to class yearmon:
"Jan 1984"
From there you can convert the yearmon class to Date (or POSIXct), like this:
x <– as.Date(x)
Now the object is of class Date:
"1984-01-01"
Define year for two digits year date format
We can create a function to do this
library(lubridate)
f1 <- function(x, year=1970){
x <- dmy(x)
m <- year(x) %% 100
year(x) <- ifelse(m > year %% 100, 2000+m, 1900+m)
x
}
f1(character_date)
#[1] "1944-01-19"
If this always have 19
as prefix for year
dmy(sub("-(\\d+)", "-19\\1", character_date))
#[1] "1944-01-19"
as.date converting dates to the wrong year
Use this format instead:mbt2$t <- as.Date(mbt2$t , format = "%d/%m/%Y")
%y
is 2-digit year, while %Y
is 4-digit year. Your version reads just first 2 digits from 1957
and that's why it returns 2019
.
Converting 2 digit date year in string to a Date Object
This seems not be implemented (yet). See the discussion here or the (open) pull request to implement it here.
It is a debated topic, as the default in other languages is to assign years >68 to the twenty century and those <=68 to the twenty-first century, that is a bit subjective, so the Julia developers preferred to go for the explicit way that the missing digits must be explicitly added.
So for now just add 2000 years:
t2 = "27/01/17"
Date(t2, "dd/mm/yy") + Dates.Year(2000)
Is there a more elegant way to convert two-digit years to four-digit years with lubridate?
Here is a function that allows you to do this:
library(lubridate)
x <- mdy(c("1/2/54","1/2/68","1/2/69","1/2/99","1/2/04"))
foo <- function(x, year=1968){
m <- year(x) %% 100
year(x) <- ifelse(m > year %% 100, 1900+m, 2000+m)
x
}
Try it out:
x
[1] "2054-01-02 UTC" "2068-01-02 UTC" "1969-01-02 UTC" "1999-01-02 UTC"
[5] "2004-01-02 UTC"
foo(x)
[1] "2054-01-02 UTC" "2068-01-02 UTC" "1969-01-02 UTC" "1999-01-02 UTC"
[5] "2004-01-02 UTC"
foo(x, 1950)
[1] "1954-01-02 UTC" "1968-01-02 UTC" "1969-01-02 UTC" "1999-01-02 UTC"
[5] "2004-01-02 UTC"
The bit of magic here is to use the modulus operator %%
to return the fraction part of a division. So 1968 %% 100
yields 68.
Character 2 digit year conversion to year only
Not elegant. But using combination of lubridate and as.Date you can get that.
library(lubridate)
data <- data.frame(variable = c(95, 96, 97,98,99), date=c(1,2,3,4,5))
data$variableUpdated <- year(as.Date(as.character(data$variable), format="%y"))
and only with base R
data$variableUpdated <- format(as.Date(as.character(data$variable), format="%y"),"%Y")
Related Topics
Efficient Way to Fill Time-Series Per Group
R Packages Fail to Compile with Gcc
How to Add Rows with 0 Counts to Summarised Output
Ggplot2: Adding Lines in a Loop and Retaining Colour Mappings
Ggplot2 Force Y-Axis to Start at Origin and Float Y-Axis Upper Limit
Choose Specific Number with Probability
Using Ggplot2 with Columns That Have Spaces in Their Names
Why Does "Hello" > 0 Return True
How to Sort a Vector of Alphanumeric Values Using Lexical Ordering in R
Dataframe Is Subseted by Row Number and Not by Cell Value After Clicking on Dt::Datatable
Manual Simulation of Markov Chain in R
Place 1 Heatmap on Another with Transparency in R
Scraping JavaScript Generated Data
Replace Na with Grouped Means in R