Home > Back-end >  How do you convert an integer into a date (format YYYY) in R
How do you convert an integer into a date (format YYYY) in R

Time:02-11

I have a big data set, filled with dates that are in integer form, and different types of integer form (YYYYMMDD, YYYYMM and YYYY)

I just want to get all of them to YYYY.

I first tried splitting up the data frame into three data frames with the different respective date (integer) forms. And then I tried to change the data form from integer to date.

Below is me splitting up the data and then all the various things i have tried and subsequently commented out.

Opera_A <- Opera_split$Date_Format_A
Opera_B <- Opera_split$Date_Format_B
Opera_C <- Opera_split$Date_Format_C


lubridate::dmy(Opera_C$Composer_Born)
#as.Date(Opera_C$Composer_Born, "%m/%d/%y")
#Composer_Born_C <- data.frame(Composer_Born)
#Opera_C <- Opera_C %>% mutate(df,dateTime=as.Date(Composer_Born, format = "%Y-%m-%d"))
#Opera_C <- transform(Opera_C,Composer_Born=as.Date(as.character(Integer),"%Y%m%d"))
# Opera_C$Composer_Born <- as.Date(Opera_C$Composer_Born, '%Y-%m-%d')

I keep getting Error in Opera_C$Composer_Born : $ operator is invalid for atomic vectors --- do i have to turn these vectors into dfs or can i just convert them directly?

Any help much appreciated --- I'm an R beginner!

Thank you

CodePudding user response:

> library(anytime)
> anydate(c(20220210, 202202, 2022))
[1] "2022-02-10" "2022-02-01" "2022-01-01"
> 

However, YYYYMM is not a really date: it is indeterminate as it could be any day of that month, ditto for YYYY. So anydate guesses for you here.

CodePudding user response:

As suggested by @caldwellst in the comments you can just pull the first 4 characters if they're all in the format you indicated. Then you can use as.Date(format = "%Y) to turn into actual date format and just fill in the M and D portions. Then use lubridate::year() to pull the year info.

library(tidyverse)
library(lubridate)

c(17701216, 177012, 1770) %>% 
  as.character() %>% 
  str_sub(1, 4) %>% 
  as.Date(format = "%Y") %>% 
  year()
#> [1] 1770 1770 1770

Created on 2022-02-10 by the reprex package (v2.0.1)

CodePudding user response:

If you want them to be recognized as dates, you could do this:

date <- as.numeric(19351225)

as.Date(paste(substr(as.character(date), 1, 4),"-01-01",sep=""),format="%Y-%m-%d")
  • Related