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.
R two digit year into four
Easiest way seems to be:
tibble(rep= "John Smith", year= 19:23, ideal_year= 2019:2023) %>%
mutate(
year = year + 2000
)
# A tibble: 5 x 3
rep year ideal_year
<chr> <dbl> <int>
1 John Smith 2019 2019
2 John Smith 2020 2020
3 John Smith 2021 2021
4 John Smith 2022 2022
5 John Smith 2023 2023
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")
R Lubridate Returns Unwanted Century When Given Two Digit Year
Lubridate v1.7.1 does not have this issue.
changing year of date for multiple observations
When you have year without century all the years from 00 to 68 are prefixed with 20 and 69 to 99 by 19. For this case you can subtract 100 years after converting the data.
library(lubridate)
as.Date(parse_date_time(df$dob, c('%d/%m/%y', '%m/%d/%y'))) - years(100)
#[1] "1948-08-02" "1934-12-13"
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)
Time difference in years with lubridate?
This is the lubridate
approach I would take:
interval(dob, today) / years(1)
Yields the answer of 32
years.
Note that the function will complain that it cannot express the remainder of the fraction of the year. This is because year is not a fixed concept, i.e. 366 in leap years and 365 in non-leap years. You can get an answer with more detail in regard to the number of weeks and days:
interval_period = interval(dob, today)
full_year = interval_period %/% years(1)
remaining_weeks = interval_period %% years(1) %/% weeks(1)
remaining_days = interval_period %% years(1) %% weeks(1) %/% days(1)
sprintf('Your age is %d years, %d weeks and %d days', full_year, remaining_weeks, remaining_days)
# [1] "Your age is 32 years, 51 weeks and 1 days"
Note that I use %/%
for division and %%
as modulo to get the remaining weeks/days after subtracting the full years/weeks.
Handling dates with mix of two and four digit years
If you know that you just have to add "19" before the dates which only have 2-digits year, you can also do it with gsub
:
vec1 <- c("06/30/97", "12/31/99", "01/01/2000", "05/25/2001")
gsub("(.*)/(..)$", "\\1/19\\2", vec1)
# [1] "06/30/1997" "12/31/1999" "01/01/2000" "05/25/2001
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"
Related Topics
How to Add a General Label to Facets in Ggplot2
Add Multiple Columns to R Data.Table in One Function Call
When Should I Use the := Operator in Data.Table
Sort Columns of a Dataframe by Column Name
Run a for Loop in Parallel in R
Split Text String in a Data.Table Columns
How to Round Up to the Nearest 10 (Or 100 or X)
Remove Rows from Data Frame Where a Row Matches a String
Growing a Data.Frame in a Memory-Efficient Manner
Force Character Vector Encoding from "Unknown" to "Utf-8" in R
How to Flatten a List of Lists
Extract Month and Year from Date in R
Converting Nested List to Dataframe
Align Ggplot2 Plots Vertically
Disable Messages Upon Loading a Package