set as date starting from 1880

Dear R users,
have one file with date starting from 188001 as an integer
e.g.
(yearmonth)

188001
188002
188003
188004
188005
.
.
.
200112

I tried to convert into date but not able to done It

I tried as


file$Year <- strptime(as.character(file$Year), "%Y%m")
file$year <- format(file$Date, "%Y")
file$month <- format(file$Date, "%m")

transform(file, x = as.Date(as.character(file$Year), "%y%m"))

But could not do it, please provide some thoughts on that.

Thank You

You can do this

library(dplyr)
library(lubridate)

# Sample data on a copy/paste friendly format
file <- data.frame(
        year = c(188001, 188002, 188003, 188004, 188005)
)

file %>% 
    mutate(year = ymd(paste0(year, "01"))) %>% 
    as_tibble() # Just to show the column class
#> # A tibble: 5 x 1
#>   year      
#>   <date>    
#> 1 1880-01-01
#> 2 1880-02-01
#> 3 1880-03-01
#> 4 1880-04-01
#> 5 1880-05-01

Created on 2020-05-05 by the reprex package (v0.3.0)

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.