Change The Year in a Datetime Object in R

Change the year in a datetime object in R?

> x <- as.Date('0014-06-30')
> x
[1] "0014-06-30"
> library(lubridate)
> year(x)
[1] 14
> year(x) <- 0
> x
[1] "0000-06-30"

How to change the year of a date POSIXct column based on thier index interval?

You could use `year<-`() from lubridate to replace the year of a datetime object.

library(dplyr)
library(lubridate)

nAUR %>%
mutate(date2 = if_else(between(date, 1484304241, 1489747821),
`year<-`(date, 2016), date))


Update
nAUR %>%
mutate(date2 = if_else(between(row_number(), which(date == 1484304241)[1], which(date == 1489747821)[1]),
`year<-`(date, 2016), date))

change years in datetime array to current year

year(tbl$time) = 2021

> tbl
# A tibble: 12 × 1
time
<dttm>
1 2021-01-05 12:00:15
2 2021-01-01 02:00:15
3 2021-02-25 12:00:15
4 2021-04-15 12:00:15
5 2021-10-15 12:00:15
6 2021-10-20 12:00:15
7 2021-11-15 12:01:15
8 2021-11-02 11:00:15
9 2021-07-09 02:00:15
10 2021-10-02 11:00:15
11 2021-01-29 12:00:15
12 2021-03-15 12:00:15

Conditionally change the year part of a date within a dplyr chain

One option is to use year function form lubridate to check and then assign back year. I have used a custom function to explain replace. This allow not to break dplyr chain and no conversion to character.

One can avoid use of custom function by use of case_when.

Option #1

replace_year <- function(x){
for(i in seq_along(x))
if(year(x[i]) == 2100){
year(x[i]) <- 2001
}
x
}

x %>% mutate(SU_BIRTH_DATE = replace_year(SU_BIRTH_DATE))

Option #2: one can avoid use of custom function with use of case_when

x %>% mutate(SU_BIRTH_DATE = case_when(
year(SU_BIRTH_DATE) == 2100 ~ `year<-`(SU_BIRTH_DATE, 2001),
TRUE ~ SU_BIRTH_DATE
))

# SU_BIRTH_DATE
# 1 2001-01-01
# 2 1977-11-24
# 3 2001-01-25
# 4 1998-08-11
# 5 1966-07-01
# 6 1976-05-13

Extract year from date

if all your dates are the same width, you can put the dates in a vector and use substring

Date
a <- c("01/01/2009", "01/01/2010" , "01/01/2011")
substring(a,7,10) #This takes string and only keeps the characters beginning in position 7 to position 10

output

[1] "2009" "2010" "2011"

How to replace the days in a date object in R?

Base R:

date <- as.Date(sub("\\d{2}$", "05", date))

Package lubridate:

lubridate::day(date) <- 5

Using lubridate function to conditionally change year in date

You could approach it like this:

library(lubridate)

dates <- seq.Date(from = as.Date('2015-01-01'),
to = Sys.Date(),
by = 'months')
indicator <- month(dates) < 12

dates2 <- dates
dates2[indicator] <- dates[indicator] %m+% years(1)

res <- data.frame(dates, indicator, dates2)
> head(res, 25)
dates indicator dates2
1 2015-01-01 TRUE 2016-01-01
2 2015-02-01 TRUE 2016-02-01
3 2015-03-01 TRUE 2016-03-01
4 2015-04-01 TRUE 2016-04-01
5 2015-05-01 TRUE 2016-05-01
6 2015-06-01 TRUE 2016-06-01
7 2015-07-01 TRUE 2016-07-01
8 2015-08-01 TRUE 2016-08-01
9 2015-09-01 TRUE 2016-09-01
10 2015-10-01 TRUE 2016-10-01
11 2015-11-01 TRUE 2016-11-01
12 2015-12-01 FALSE 2015-12-01
13 2016-01-01 TRUE 2017-01-01
14 2016-02-01 TRUE 2017-02-01
15 2016-03-01 TRUE 2017-03-01
16 2016-04-01 TRUE 2017-04-01
17 2016-05-01 TRUE 2017-05-01
18 2016-06-01 TRUE 2017-06-01
19 2016-07-01 TRUE 2017-07-01
20 2016-08-01 TRUE 2017-08-01
21 2016-09-01 TRUE 2017-09-01
22 2016-10-01 TRUE 2017-10-01
23 2016-11-01 TRUE 2017-11-01
24 2016-12-01 FALSE 2016-12-01
25 2017-01-01 TRUE 2018-01-01

Extract Month and Year From Date in R

This will add a new column to your data.frame with the specified format.

df$Month_Yr <- format(as.Date(df$Date), "%Y-%m")

df
#> ID Date Month_Yr
#> 1 1 2004-02-06 2004-02
#> 2 2 2006-03-14 2006-03
#> 3 3 2007-07-16 2007-07

# your data sample
df <- data.frame( ID=1:3,Date = c("2004-02-06" , "2006-03-14" , "2007-07-16") )

a simple example:

dates <- "2004-02-06"

format(as.Date(dates), "%Y-%m")
> "2004-02"

side note:
the data.table approach can be quite faster in case you're working with a big dataset.

library(data.table)
setDT(df)[, Month_Yr := format(as.Date(Date), "%Y-%m") ]


Related Topics



Leave a reply



Submit